CSharp - Generic Type Parameters


Type parameters can be introduced to a type by classes, structs, interfaces, delegates, and methods.

Other constructs, such as properties and indexer, cannot introduce a type parameter but can use one.

For example, the property Value uses T:

struct Nullable<T>
       public T Value { get; }

A generic type or method can have multiple parameters. For example:

class HashTable<TKey, TValue> {

To instantiate:

HashTable<int,string> myDic = new HashTable<int,string>();


var myDic = new HashTable<int,string>();

Generic type names and method names can be overloaded as long as the number of type parameters is different.

For example, the following three type names do not conflict:

class A{
class A<T>{
class A<T1,T2> {