Search This Blog

C# Generics – (Generic Classes)


Generics:
Generics are very powerful and exciting feature of C#. It allows you to create, store and manipulate type-safe data without compromising performance and productivity. Suppose you want to create a list of integers, and perform some operations on that list, at the same time you want to use same class for storing strings and want to perform same operations (w.r.t. strings). What type of class you will create ?
A simple answer would be that we may go to route where we are storing everything as "object" which is base class for every data-type in C#. Hmm thats right, but there is even better way... i.e. GENERICS.
For C++ guys, generics would be like "C++ templates", but I would say that "C# Generics" are super-set of "C++ template"

Following are steps to create a generic class:


  1. Use any letter/word as "future data type" of object. [In our following example we are using "T", can not use keywords here...]
  2. Declare member variables/properties of type "T"
  3. Actual data-type of "T" will be determined at time of object declaration.
  4. Use "is" keyword to check data type of "T" at runtime.
Following is class: which has data type "T" (determined on object initialization)
public class MyGenericClass<T> {
   public T myData;
   public string GetResult()
   {
      string genericResult = "";
      if (myData is string
      {
         genericResult = "C# Generic Example : You can only perform string operations";
      }
      else if (myData is Int32)
      {
         genericResult = "C# Generic Example : You can only perform Int32 operations";
      }
      else 
      {
         genericResult = "C# Generic Example : This type is not supported by our Generic Class";
      }
      return genericResult;
   }
}



Following is test program to test this C# generic example:
public class Program
{
   static void Main(string[] args)
   {
      MyGenericClass<string> strClassObject = new
      MyGenericClass<string>();
      strClassObject.myData = "C# Generic Example";


      MyGenericClass<Int32> int32ClassObject = new MyGenericClass<Int32>();
      int32ClassObject.myData = 4;

      Console.WriteLine("Generic class of string type : {0}"  strClassObject.GetResult());

      Console.WriteLine("Generic class of Int32 type : {0}",int32ClassObject.GetResult());

      Console.ReadKey();
   }
}


And output of the above program is:
Generic class of string type : C# Generic Example : You can only perform string operations
Generic class of Int32 type : C# Generic Example : You can only perform Int32 operations


Multiple Unknown Data-types:
Suppose if there are multiple 'un-known data types' , even then it is supported by "C# generics", for example if a class should have 'an integer and two unknown types' then class may look like:
public class MySecondGenericClass
{
   public Int32 data1;
   public T data2;
   public T data3;
   public M data4;
}
And object declaration of this class may look like:
MySecondGenericClass<string, float> myComplexGenericClass = new MySecondGenericClass<string, float>();
In above example "T" is considered as "string", hence data-type of 'data2' and 'data3' is 'string', whereas "M" is considered as "float" and data type of 'data4' is considered as 'float', whereas data1 will always be of type 'Int32'


There is a lot more that C# Generics offer, like impacts/restrictions on current and inherited classes, which we will share in next blog…. Keep reading http://www.code4coder.com
J




 

No comments:

Post a Comment