Declaring Type Parameters - CSharp Custom Type

CSharp examples for Custom Type:Generics

Introduction

Type parameters can be introduced in the declaration of classes, structs, interfaces,delegates, and methods.

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

For example, the property Value uses T:

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

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

class Dictionary<TKey, TValue> {...}

To instantiate:

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

Or:

var myDic = new Dictionary<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> {}

Related Tutorials