C# Type Parameters
Description
Type parameters can be introduced in the declaration of classes, structs, interfaces, delegates, and methods.
class List<TValue> {}
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; set; }
}
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 two type names do not conflict:
class A<T> {}
class A<T1,T2> {}
Next chapter...
What you will learn in the next chapter:
- A list of preprocessor commands available in C#
- All C# Preprosessor directives
- Example for Preprocessor directives