Generic Overview
You can use generic types to maximize code reuse, protect type security, and improve performance.
The most common use of generics is to create a collection class.
The. NET Framework class library contains several new Generic collection classes in the System. Collections. Generic namespace. Use these classes as much as possible to replace common classes, such as the ArrayList in the System. Collections namespace.
You can create your own generic interfaces, generic classes, generic methods, generic events, and generic delegation.
You can restrict generic classes to access specific data types.
Information about types used in generic data types can be obtained at runtime through reflection.
Advantages
Both generic and generic methods have reusability, type security, and efficiency, which are not possible for non-generic and non-generic methods. Generics are usually used together with collections and Methods acting on collections .. NET Framework 2.0 Class Library provides a new namespace System. Collections. Generic, which contains several new Generic-based collection classes. We recommend that you use new generic collection classes for all applications of. NET Framework 2.0 and later, instead of using old non-generic collection classes such as ArrayList.
When to use a generic set
Generally, we recommend that you use a generic set, because this provides direct advantages of type security without deriving from the base set type and implementing type-specific members. In addition, if the set element is of the value type, the performance of the generic set type is generally better than that of the corresponding non-generic set type (and better than the type derived from the non-generic base set type ), because you do not need to pack elements when using generics.
The following generic types correspond to the existing collection types:
List <(Of <(T>)> is a generic class corresponding to ArrayList.
Dictionary <(Of <(TKey, TValue>) is a generic class corresponding to Hashtable.
Collection <(Of <(T>)> is a generic class corresponding to CollectionBase. Collection <(Of <(T>) can be used as a base class, but unlike CollectionBase, It is not abstract. This is much easier to use.
ReadOnlyCollection <(Of <(T>) is a generic class corresponding to ReadOnlyCollectionBase. ReadOnlyCollection <(Of <(T>) is not abstract. It has a constructor, this constructor makes it easy to publish an existing List <(Of <(T>)> As a read-only set.
Queue <(Of <(T>), Stack <(Of <(T>), and SortedList <(Of <(TKey, TValue>) generic classes correspond to non-generic classes with the same name respectively.
Other Types
There are several generic set types that do not have corresponding non-generic types:
Partition list <(Of <(T>)> is a list Of common links. It provides insert and remove operations with the computing complexity Of O (1.
SortedDictionary <(Of <(TKey, TValue>)> Is a sort dictionary. Its insert and search operations are computed in the O (log n ), this makes it a very useful alternative type for SortedList <(Of <(TKey, TValue>.
KeyedCollection <(Of <(TKey, TItem>)> is a hybrid type between lists and dictionaries. It provides a method to store objects containing their own keys.
LINQ to Objects
The LINQ to Objects function allows you to use LINQ to query and access Objects in the memory, provided that the object type must be IEnumerable or IEnumerable <(Of <(T>)> ). Compared with the standard foreach loop, a general data access mode is provided for a LINQ query, which is more concise and readable. This type of query supports filtering, sorting, and grouping. High performance can also be improved by using LINQ queries. For more information, see LINQ to Objects.
Other functions
Some generic types have functions not available in non-generic set types. For example, the List <(Of <(T>)> class (corresponding to the non-generic ArrayList class) there are many Predicate that accept generic delegation (for example, Predicate <(Of <(T>) delegate, indicating the Action <(Of <(T>)> Of each list element) that allows conversion between the delegate and the allowed definition type <(Of <(TInput, TOutput>) delegate.
The List <(Of <(T>)> class allows you to specify your own IComparer <(Of <(T>) for sorting and searching lists. SortedDictionary <(Of <(TKey, TValue>) and SortedList <(Of <(TKey, TValue>) classes also provide this function, you can also specify a comparator when creating a set. Similarly, the Dictionary <(Of <(TKey, TValue>)> and KeyedCollection <(Of <(TKey, TItem>)> classes allow you to specify your own equal comparator.
Reference: Introduction to generics in CLR http://msdn.microsoft.com/zh-cn/library/aa686046.aspx