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:

  1. A list of preprocessor commands available in C#
  2. All C# Preprosessor directives
  3. Example for Preprocessor directives
Home »
  C# Tutorial »
    C# Types »
      C# Generics
C# Generic Types
C# Generic Methods
C# default Generic Value
C# Generic Constraints
C# Subclassing Generic Types
C# Generic Delegate
C# Type Parameters