Sunday, May 29, 2016

Introduction to Generic in C#

Generic introduced with version 2.0 of C# language. Generics introduce to the .NET Framework the concept of "Type Parameters", which make it possible to design classes and methods that defer 
the specification of one or more type until the class or method is declared and instantiated by client code.

Generic classes and methods combine re-usability, type safety and efficiency in a way that their non generic counterpart cannot. Like wise we can say benefits for generic can listed as below.

Benefits of Generic
  1.  Allow write code  library method which are type-safe.
  2. Because of generic being used, compiler can perform compile-time checks on code for type safety.
  3. Faster than using objects as it either avoids boxing/un-boxing or casting from objects to the required reference type.
  4. Allow you to write code which is applicable to many type with the same underlying behavior.
Generic Type Parameters

In a generic type or method definition, a type parameter is a placeholder for a specific type that a client specifies when they instantiate a variable of the generic type.

Example: A generic class List<T>, cannot be used as it is since it is not really type. It is like blueprint of a type. client code must declare and instantiate a constructed type by specifying a type argument inside the angle brackets. 
The type argument for this particular class can be any type recognized by the compiler. 
(Microsoft)

Type Parameter Naming Guideline
  • Do name generic type parameters with descriptive names, unless a single letter name is completely self explanatory and a descriptive name would not add value.
 public interface ISessionChannel<TSession>
  • Consider using T as the type parameter name for types with one single letter type parameter.
 public int IComparer<T>() { return 0; }
  • Do prefix descriptive type parameter names with "T".
 public interface ISessionChannel<TSession>
        {
          TSession Session { get; }
         }
  • Consider indicating constraints placed on a type parameter in the name of parameter. For example, a parameter constrained to ISession may be called TSession.
Constraints on Type Parameters
C# allow you to add restrictions to the kinds of types that can client code can use for type arguments when you instantiates your generic class.To perform this task you need to use where contextual keyword. According to Microsoft you can apply six type of constraints.
  •  where T: struct  - The type argument must be value type except Nullable.
  • where T: class - The type argument must be reference type(class,delegate,interface or array type).
  • where T: new() - The type argument must have a public parameter-less constructor.
  • where T: <base class name> -The type argument must be or derive from the specified base class.
  • where T: <interface name> - The type argument must be or implement the specified interface/s.
  • where T: U - The type argument supplied for T must be or derive from argument supplied for U.
In next tutorial we will implement repository design pattern using generic.