Reference Type Constraint: A reference type constraint restricts a type parameter to a reference type.

image_pdfimage_print
   
 
using System;
using System.Collections;


public class Starter {
    public static void Main() {
        // MyClass<int> obj1=new MyClass<int>(); [illegal]
        MyClass<XClass> obj2 = new MyClass<XClass>();

    }
}

public class MyClass<T> where T : class {

    public void Iterate(T data) {
    }
}

public class XClass {
}

    


This entry was posted in Generics. Bookmark the permalink.