Generic Delegates - CSharp Custom Type

CSharp examples for Custom Type:Generics

Description

Generic Delegates

Demo Code

using System;/*from  www  .j a va  2 s . c om*/
using System.Collections.Generic;
delegate T IntConverter<T>(T n);
class TestDelegate {
   static int num = 10;
   public static int AddNum(int p) {
      num += p;
      return num;
   }
   public static int MultNum(int q) {
      num *= q;
      return num;
   }
   public static int getNum() {
      return num;
   }
   static void Main(string[] args) {
      IntConverter<int> nc1 = new IntConverter<int>(AddNum);
      IntConverter<int> nc2 = new IntConverter<int>(MultNum);
      //calling the methods using the delegate objects
      nc1(25);
      Console.WriteLine("Value of Num: {0}", getNum());
      nc2(5);
      Console.WriteLine("Value of Num: {0}", getNum());
   }
}

Result


Related Tutorials