Csharp/CSharp Tutorial/delegate/Anonymous delegate — различия между версиями

Материал из .Net Framework эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Текущая версия на 12:19, 26 мая 2010

An anonymous method.

using System;  
  
delegate void Do();  
  
class AnonMethDemo {  
 
  public static void Main() {   
     Do count = delegate {  
      for(int i=0; i <= 5; i++)  
        Console.WriteLine(i); 
    }; 
 
    count();  
  } 
}
0
1
2
3
4
5

An anonymous method that returns a value.

using System;  
  
delegate int CountIt(int end);  
  
class MainClass {  
 
  public static void Main() {   
    CountIt count = delegate (int end) { 
      Console.WriteLine("end:"+end);
      return end;
    }; 
 
    int result = count(3); 
    Console.WriteLine("Summation of 3 is " + result); 
 
    result = count(5);  
    Console.WriteLine("Summation of 5 is " + result); 
  } 
}
end:3
Summation of 3 is 3
end:5
Summation of 5 is 5

An anonymous method that takes an argument.

using System;  
  
delegate void CountIt(int end);  
  
class MainClass {  
 
  public static void Main() {   
    CountIt count = delegate (int end) { 
        Console.WriteLine("end:"+end); 
    }; 
 
    count(3); 
    count(5);  
  } 
}
end:3
end:5

Anonymous delegate method

using System;
class MainClass
{
   delegate int FunctionToCall(int InParam);
   static void Main()
   {
      FunctionToCall del = delegate(int x){
                        return x + 20;
                     };
      Console.WriteLine("{0}", del(5));
      Console.WriteLine("{0}", del(6));
   }
}
25
26

Anonymous delegate methods

using System;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.InteropServices;
delegate int IntDelegate(int x);
public class MainClass
{
    static void TransformUpTo(IntDelegate d, int max)
    {
        for (int i = 0; i <= max; i++)
            Console.WriteLine(d(i));
    }
   public static void Main(){
        TransformUpTo(delegate(int x) { return x * x; }, 10);
   }
}
0
1
4
9
16
25
36
49
64
81
100

anonymous method delegate invocation

using System;
using System.Collections.Generic;
using System.Text;
    class Program
    {
        delegate void MessagePrintDelegate(string msg);
        static void Main(string[] args)
        {

            MessagePrintDelegate mpd2 = delegate(string msg)
            {
                Console.WriteLine("[Anonymous] {0}", msg);
            };
            LongRunningMethod(mpd2);
        }
        static void LongRunningMethod(MessagePrintDelegate mpd)
        {
            for (int i = 0; i < 99; i++)
            {
                if (i % 25 == 0)
                {
                    mpd(string.Format("Progress Made. {0}% complete.", i));
                }
            }
        }
        static void PrintMessage(string msg)
        {
            Console.WriteLine("[PrintMessage] {0}", msg);
        }
    }

Anonymous Methods

using System;
using System.Threading;

public delegate void DelegateClass();
public class Starter {
    public static void Main() {
        DelegateClass del = delegate {
            Console.WriteLine("Running anonymous method");
        };
        del();
    }
}

Create a delegate by declaration

using System;
using System.Collections.Generic;
using System.Text;
class Program {
    delegate string delegateTest(string val);
    static void Main(string[] args) {
        string mid = ", middle part,";
        delegateTest d = delegate(string param) {
            param += mid;
            param += " and this was added to the string.";
            return param;
        };
        Console.WriteLine(d("Start of string"));
    }
}

Demonstrate a captured variable.

using System;  
  
delegate int CountIt(int end);
  
class MainClass {  
 
  static CountIt counter() { 
    CountIt ctObj = delegate (int end) { 
      Console.WriteLine("end:"+end);
      return end; 
    }; 
    return ctObj; 
  } 
 
  public static void Main() {   
    CountIt count = counter(); 
 
    int result = count(3); 
    result = count(5);  
  } 
}
end:3
end:5