CSharp OOP - Generics

/*

Author: Jiangong SUN

*/

Generics is introduced by .NET Framework 2.0, it's most commonly used with collections and the methods that operate on them. 

Namespace: System.Collections.Generic


There are two forms of generics in C#: generic types (including classes, interfaces, delegates, and structures—there are no generic enums) and generic methods.

In most cases, it is recommended that you use the List<T> class provided by the .NET Framework class library, rather than create your own.


If you ever need to describe a generic type to a colleague, it’s conventional to use “of” to introduce the type parameters or arguments—so List<T> is pronounced “list of T”.


Generic types can effectively be overloaded on the number of type parameters—so you could define MyType, MyType<T>, MyType<T,U>, MyType<T,U,V>, and so forth, all
within the same namespace.


Generic List vs. ArrayList:

- ArrayList will box all value types to Object when adding to it, and unbox Object to value types when retrieving from it, which will reduce the performance.

- ArrayList cast all types of data to Object, and it doesn't check errors in compile time.

See my post for the comparison



Generic Class:


Generic Interface:

Generic Delegates:

Generic Methods:




你可能感兴趣的:(generics,C#)