A simple generic class : Generic Class : Generics C# Source Code


Custom Search

C# Source Code » Generics » Generic Class »

 

A simple generic class









    

using System;

class MyGenericClass<T> {
  T ob;

  public MyGenericClass(T o) {
    ob = o;
  }

  public T getob() {
    return ob;
  }

  public void showType() {
    Console.WriteLine("Type of T is " + typeof(T));
  }
}

public class Test {
  public static void Main() {
    MyGenericClass<int> iOb;

    iOb = new MyGenericClass<int>(102);

    iOb.showType();

    int v = iOb.getob();
    Console.WriteLine("value: " + v);

    MyGenericClass<string> strOb = new MyGenericClass<string>("Generics add power.");
    strOb.showType();
    
    string str = strOb.getob();
    Console.WriteLine("value: " + str);
  }
}

           
       
    
   
  
   







HTML code for linking to this page:

Follow Navioo On Twitter

C# Source Code

 Navioo Generics
» Generic Class