C# Generics

by Ismail Sirma

Posted on 11.7.2018 21:39:13

C# Generics

Generic types make it easier to program without thinking of data type elements in a class or a method. You may have a class that can work any or limited types of classes.


When a generic class constructor or a generic method is being called code is generated with parameter types that are passed in. Before constructor being called, it does not need to know the type.


Here is an example:


public class GenericDictionary<TKey, TValue>


private TKey[] sampleArrayKeys;

private TValue[] sampleArrayValues;

public GenericDictionary(int size)


sampleArrayKeys = new TKey[size];

sampleArrayValues = new TValue[size];


public void add(TKey key, TValue value)


// increase array size

Array.Resize(sampleArrayKeys, sampleArrayKeys.Length+1);

Array.Resize(sampleArrayValues, sampleArrayValues.Length+1);

// assign the keys and values in same order

sampleArrayKeys[sampleArrayKeys.Length-1] = key.ToString().GetHashCode()

sampleArrayValues[sampleArrayValues.Length-1] = value;





Generics Features:

* It helps wtih code duplication by simply avoiding repeatative code

* Less code means less code to fix

* Giving object with a reference, it is boxed to be stored as an object and unboxed to access it. This causes performance problem. Generics solve the issue and increases the performance.

* Generics allows type safety

* You may create generic classes with particular data type boundaries.

* You have option to create generic classes, methods, delegates, events or interfaces


Generic Method Example:

If a constraint is not defined, any type of input object can be passed into a generic method.


public class Example 


public void Add(TKey key, TValue value)


// Do sth




C# Generics Consraints:


Consraints define boundaries of type parameters in an instance of a generic type. If any type that is not viable to constraint will cause compile time error. Constraints can be easily applied by adding a clause at the end of class name : "where T : struct"


You have option to add multiple constraints in a generic class

"where T : IComparable, new()"


Several constraints:


"where T: <interface name>" interface constraint such as IComparable

"where T: struct"  value type constraint

"where T: class" reference type constraint

"where T: new()" default parameter constraint, T is an object with default constructor. So you can initiate instance of object inside class.


An example:


class GenericClass<T> where T: struct, new()


    private T _genericMemberVariable;

    public GenericClass(T value)


        _genericMemberVariable = value;


    public T genericMethod(T genericParameter)


        Console.WriteLine("Parameter type: {0}, value: {1}", typeof(T).ToString(),genericParameter);


        return _genericMemberVariable;


    public T genericProperty { get; set; }

    public bool HasValue


    get { return _genericMemberVariable != null; }


    public T GetValueOrDefault()



    return (T) _genericMemberVariable;

    return default(T);


    // where T: new() constraint allows you to create a new istance

    public void DoSomething(T Value)


    var obj = new T();



Blog Search


Welcome to Blog of Ismail Sirma.

Back to List