Demonstrate the default keyword

image_pdfimage_print


   
 

using System;

class MyClass {
}

class Test<T> {
  public T obj;

  public Test() {
    obj = default(T);
  }
}

class DefaultDemo {
  public static void Main() {
    Test<MyClass> x = new Test<MyClass>();

    if(x.obj == null)
      Console.WriteLine("x.obj is null.");

    Test<int> y = new Test<int>();

    if(y.obj == 0)
      Console.WriteLine("y.obj is 0.");
  }
}
           
         
     


This entry was posted in Generics. Bookmark the permalink.