Overview Of Generic Types In C#

Introduction
  1. At run-time May get information on the types used in a generic type .
  2. We can create generic classes by starting with pre define class.
  3. We may create our own generic classes.
  4. Generics started from framework 2.0. 
  5. Generics allow we to define a class with a replacement holder for the type of its fields, methods, parameters, etc. 
  6. Generics replace these replacement holder with some specific type at compile time.
  7. A generic class can be defined using angle brackets <>
Description
 
For example, the letter G denotes a type that is only known based on the calling location.The program can act upon the instance of G like it is a real type, but it is not(In in different sites G refer to T which denotes to Type That may be GT or other)
 
The Base Class library in c# for Generic is System.Collections.Generic;
 
Bellow is the Example,
 
In the Introduction I introduced the generic types may used in the method,property and parameters of a class .The bellow example is narates all these.
  1. class GenericTest<G>    
  2.     {    
  3.         G _value;    
  4.     
  5.         public GenericTest(G t)    
  6.         {    
  7.         // equal type as the parameter.    
  8.         this._value = t;    
  9.         }    
  10.     
  11.         public void Writevalue()    
  12.         {    
  13.         Console.WriteLine(this._value);    
  14.         }    
  15.     }    
  16. }    
  17.     
  18. //In the Introduction I introduced the generic types may used in the method,property and parameters of a class .The bellow example is narates all these .  
  19.   
  20. class GenericClassDemo<G>    
  21.     {    
  22.         //Member variable of Generic type    
  23.         private G MemberVariable;    
  24.     
  25.         //Here  used as a parameter    
  26.         public GenericClassDemo(G value)    
  27.         {    
  28.             MemberVariable = value;    
  29.         }    
  30.     
  31.         public G Method(G Parameter)    
  32.         {    
  33.             Console.WriteLine("Generic Parameter : " + typeof(G).ToString() + Parameter.ToString());    
  34.             Console.WriteLine("Return Generic type: " + typeof(G).ToString() + MemberVariable.ToString());    
  35.     
  36.             return MemberVariable;    
  37.         }    
  38.         //Define a property with generic type    
  39.         public G Property { get; set; }    
  40.     }    
  41.     
  42.  //The bellow example is narates the generic array    
  43.     
  44.   public class GenericArray<G>    
  45.     {    
  46.         private G[] array;    
  47.     
  48.         public GenericArray(int size)    
  49.         {    
  50.             array = new G[size + 1];    
  51.         }    
  52.         public G getItem(int index)    
  53.         {    
  54.             return array[index];    
  55.         }    
  56.         public void setItem(int index, G value)    
  57.         {    
  58.             array[index] = value;    
  59.         }    
  60.     }    
The Generic Used Classes are Calling in main method those are define bellow,
  1. class Program  
  2.    {  
  3.        static void Main(string[] args)  
  4.        {  
  5.            //  int type parameter.  
  6.            GenericTest<int> gt1 = new GenericTest<int>(50);  
  7.            // Calling of the Writevalue method.  
  8.            gt1.Writevalue();  
  9.   
  10.            // string type parameter.  
  11.            GenericTest<string> gt2 = new GenericTest<string>("c # corner");  
  12.            gt2.Writevalue();  
  13.   
  14.            callingarryandaclass();  
  15.            Console.ReadLine();  
  16.        }  
  17.   
  18.        static void callingarryandaclass()  
  19.        {  
  20.            GenericArray<double> doubleArray = new GenericArray<double>(5);  
  21.   
  22.            //setting values  
  23.            for (int c = 0; c < 5; c++)  
  24.            {  
  25.                doubleArray.setItem(c, c * 5);  
  26.            }  
  27.   
  28.            //retrieving the values  
  29.            for (int c = 0; c < 5; c++)  
  30.            {  
  31.                Console.Write(doubleArray.getItem(c) + " ");  
  32.            }  
  33.   
  34.            Console.WriteLine();  
  35.   
  36.            GenericClassDemo<int> GenericDemo = new GenericClassDemo<int>(10);  
  37.   
  38.            int val = GenericDemo.Method(100);  
  39.   
  40.            Console.WriteLine("value :" + val);  
  41.        }  
  42.    }  
Generics can also applied to Interface,Abstract class,Static method,Event,Delegates and Operator.
 
Summery
 
Generic removes the possibilities of boxing and unboxing and Like Reflection At run-time May get information on the types.