A simple generic delegate

image_pdfimage_print


   


using System;

// Declare a generic delegate.
delegate T GenericDelegate<T>(T v);

class GenDelegateDemo {
  static int sum(int v) {
    int result = 0;
    for(int i=v; i>0; i--)
      result += i;

    return result;
  }

  static string reflect(string str) {
    string result = "";

    foreach(char ch in str)
      result = ch + result;

    return result;
  }

  public static void Main() {
    GenericDelegate<int> intDel = sum;
    Console.WriteLine(intDel(3));

    GenericDelegate<string> strDel = reflect;
    Console.WriteLine(strDel("Hello"));
  }
}
           
          


constructor constraint

image_pdfimage_print
   
 

using System;


public class Starter {
    public static void Main() {
        MyClass obj = new MyClass();
        obj.MethodA<XClass>();
    }
}

public class MyClass {
    public void MethodA<T>()
                 where T : XClass, new() {
        Console.WriteLine("MyClass.MethodA");
        T obj = new T();
        obj.MethodB();
    }
}

public class XClass {
    public void MethodB() {
        Console.WriteLine("XClass.MethodB");
    }
}

    


Derivation Constraint

image_pdfimage_print
   
 
using System;

public class Starter {
    public static void Main() {

        // good
        MyClass<XClass, YClass> obj = new MyClass<XClass, YClass>();

        // good
        MyClass<XClass, WClass> obj2 = new MyClass<XClass, WClass>();

        // bad
        MyClass<WClass, YClass> obj3 = new MyClass<WClass, YClass>();

    }
}

public class MyClass<K, V>
    where K : XClass
    where V : YClass {
}

public class XClass {

}

public class YClass {
}

public class WClass : YClass {
}

    


Demonstrate a value type constraint

image_pdfimage_print
   


using System;

struct MyStruct {
}

class MyClass {
}

class Test<T> where T : struct {
  T obj;

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

class Test {
  public static void Main() {
    Test<MyStruct> x = new Test<MyStruct>(new MyStruct());
    Test<int> y = new Test<int>(10);

    // But, the following declaration is illegal!
//    Test<MyClass> z = new Test<MyClass>(new MyClass());
  }
}
           
          


Demonstrate a reference constraint

image_pdfimage_print
   


using System;
class MyClass {
}

class Test<T> where T : class {
  T obj;

  public Test() {
    // The following statement is legal only
    // because T is guaranteed to be a reference
    // type, which can be assigned the value null.
    obj = null;
  }
  public void print(){
     Console.WriteLine(obj);
  }
}

class ClassConstraintDemo {
  public static void Main() {

    Test<MyClass> x = new Test<MyClass>();

    // The next line is in error because int is
    // a value type.
//    Test<int> y = new Test<int>();
  }
}
           
          


A new() constructor constraint

image_pdfimage_print
   


using System;

class MyClass {

  public MyClass() {
  }

}

class Test<T> where T : new() {
  T obj;

  public Test() {
    // This works because of the new() constraint.
    obj = new T(); // create a T object
  }
}

class ConsConstraintDemo {
  public static void Main() {

    Test<MyClass> x = new Test<MyClass>();

  }
}