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> {}




















Home »
  C# Tutorial »
    Custom Types »




C# Class
C# Struct
C# Interface
C# Inheritance
C# Namespace
C# Object
C# Delegate
C# Lambda
C# Event
C# Enum
C# Attribute
C# Generics
C# Preprocessor