예제 #1
0
        static void Main(string[] args)
        {
            TakeIntReturnIntDelegate method1 =
                delegate(int v) {
                return(v + 100);
            };

            DoSomethingWithDelegate(method1);

            DoSomethingWithDelegate(delegate(int x) { return(x / 2); });
        }
예제 #2
0
        static void Main(string[] args)
        {
            // Doesn't work
            //TakeIntReturnIntDelegate del = Foo;

            // Hm... shouldn't work, but does :) -
            // Impossible to configure the C# compiler these days to NOT understand this.
            TakeIntReturnIntDelegate del = Square;

            // How it should have been in C# 1.0:
            del = new TakeIntReturnIntDelegate(Square);

            Console.WriteLine(del(13));

            // Delegates are types - do with them what you can do with other types.
            DoSomethingWithDelegate(del);
        }
예제 #3
0
 static void DoSomethingWithDelegate(TakeIntReturnIntDelegate del)
 {
     Console.WriteLine(del(5) + del(8));
 }
예제 #4
0
 static void DoSomething(TakeIntReturnIntDelegate function)
 {
     Console.WriteLine(function(10));
 }