Generic Classes In C#

  1. namespace Consolepractice   
  2. {  
  3.     class Program {  
  4.         public static void Main(string[] args) {  
  5.             // Use the generic type Demo with an int type parameter.  
  6.             Demo < int > test1 = new Demo < int > (2);  
  7.             // Call the Write method.  
  8.             test1.print();  
  9.             // Use the generic Demo with a string type parameter.  
  10.             Demo < string > test2 = new Demo < string > ("soumalya das");  
  11.             test2.print();  
  12.             // Use the generic Demo with a float type parameter.  
  13.             Demo < float > test3 = new Demo < float > (4.5 f);  
  14.             test3.print();  
  15.             Console.ReadKey();  
  16.         }  
  17.     }  
  18.     class Demo < T > {  
  19.         T _value;  
  20.         public Demo(T t) {  
  21.             // The field has the same type as the parameter.  
  22.             this._value = t;  
  23.         }  
  24.         public void print() {  
  25.             Console.WriteLine(this._value);  
  26.         }  
  27.     }