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

            DoSomethingWithDelegate(method1);

            DoSomethingWithDelegate(delegate(int x) { return(x / 2); });
        }
Beispiel #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);
        }
Beispiel #3
0
 static void DoSomethingWithDelegate(TakeIntReturnIntDelegate del)
 {
     Console.WriteLine(del(5) + del(8));
 }
Beispiel #4
0
 static void DoSomething(TakeIntReturnIntDelegate function)
 {
     Console.WriteLine(function(10));
 }