Anonymous Methods - CSharp Custom Type

CSharp examples for Custom Type:Anonymous Types

Description

Anonymous Methods

Demo Code

using System;/*from  w  w  w  . ja v a2  s . c  om*/
delegate void IntConverter(int n);
class TestDelegate {
   static int num = 10;
   public static void AddNum(int p) {
      num += p;
      Console.WriteLine("Named Method: {0}", num);
   }
   public static void MultNum(int q) {
      num *= q;
      Console.WriteLine("Named Method: {0}", num);
   }
   public static int getNum() {
      return num;
   }
   static void Main(string[] args) {
      //create delegate instances using anonymous method
      IntConverter nc = delegate(int x) {
         Console.WriteLine("Anonymous Method: {0}", x);
      };
      nc(10);
      nc =  new IntConverter(AddNum);
      nc(5);
      nc =  new IntConverter(MultNum);
      nc(2);
   }
}

Result


Related Tutorials