Mega Code Archive

 
Categories / C# Book / 01 Language Basics
 

0178 Type parameters

Only method, class, interfce, delegate and struct can have type parameters. Other member can only reference them. using System; class Wrapper<T> { T t; T get() { return t; } void set(T t) { this.t = t; } } class Test { static void Main() { } } The following example takes two generic type parameters. using System; class Pair<TKey, TValue> { TKey key; TValue value; TKey getKey() { return key; } void setKey(TKey key) { this.key = key; } TValue getValue() { return value; } void setValue(TValue v) { this.value = v; } } class Test { static void Main() { } }