C# and .Net Delegates

Please read following simple source to know more about delegates in C# and .Net. Note that this article is for people who wants to learn by looking at simple sample source code without reading too much weed.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
using System;
namespace Delegates
{
    class Program
    {
        //Following is delegate capable to refer to method taking two int para and returning int.
        delegate int SimpleDelegate(int i, int j);//SimpleDelegate is a type
        int Subtract(int i, int j) { return i - j; }
        static int Sum(int a, int b) { return a + b; }
        static void Main()
        {
            int res;
            SimpleDelegate dlgt;
            Program p = new Program();

            dlgt = Sum;//Assign static method reference.
            res = dlgt(20, 10);//Invoke delegate. Equivalent to Sum(20, 10)
            Console.WriteLine("n1. Result : " + res);

            dlgt = p.Subtract;//Assign non-static method reference. Need object.
            res = dlgt(20, 10);//Invoke delegate. Equivalent to p.Subtract(20, 10)
            Console.WriteLine("n2. Result : " + res);

            dlgt = delegate(int i, int j) { return i * j; };//assign anonymous method
            res = dlgt(20, 10);//Invoke delegate
            Console.WriteLine("n3. Result : " + res);

            //lambda exression: X => Y
            //X is list of input, Y can be expression.
            dlgt = (i, j) => i / j;//assign using lambda expression
            res = dlgt(20, 10);//Invoke delegate
            Console.WriteLine("n4. Result : " + res);

            //Action<> is generic delegate taking N type-para.
            //Action can only refer to method taking N para of specified types and returning void.
            Action<int, int> print = (i, j) => Console.WriteLine("n5. Remainder : " + i % j);
            print(20, 10);

            //Predicate<> is generic delegate taking 1 type-para.
            //Predicate can only refer to method taking 1 parameter of specified type and returning bool.
            Predicate<int> IsFive = i => i == 5;
            Console.WriteLine("n6. Result : " + IsFive(20));

            //Func<> is generic delegate taking N type-para.
            //Func can only refer to method taking N-1 para of specified type and returning value of Nth type.
            Func<int, int> f1 = i => i + 1;
            Console.WriteLine("n7. Result : " + f1(20));

            //Comparison<> is generic delegate taking 1 type-para.
            //Comparison can only refer to method taking 2 para of same type and returning int.
            //Method must return -ve, 0 or +ve as a result of comparison
            Comparison<int> comp = (i, j) => i < j ? -1 : (i > j ? 1 : 0);
            Console.WriteLine("n8. Result : " + comp(20, 10));

            //Converter<> is generic delegate taking 2 type-para.
            //Converter can only refer to method taking 1 para of first type
            //  and returning converted result of second type.
            //Following converts string to bool.
            Converter<string, bool> convert = s => Convert.ToBoolean(s);
            Console.WriteLine("n9. Result : " + (convert("true") == true ? "Conversion done" : "???"));

            Console.ReadLine();
        }
    }
}
  • RSS
  • Print
  • PDF
  • Twitter
  • del.icio.us
  • Facebook
  • LinkedIn
  • Google Bookmarks
  • Digg
  • Add to favorites
  • StumbleUpon

Post a Comment

Your email is never published nor shared. Required fields are marked *