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

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

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

An anonymous method.

<source lang="csharp">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();  
 } 

}</source>

0
1
2
3
4
5

An anonymous method that returns a value.

<source lang="csharp">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); 
 } 

}</source>

end:3
Summation of 3 is 3
end:5
Summation of 5 is 5

An anonymous method that takes an argument.

<source lang="csharp">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);  
 } 

}</source>

end:3
end:5

Anonymous delegate method

<source lang="csharp">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));
  }

}</source>

25
26

Anonymous delegate methods

<source lang="csharp">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);
  }

}</source>

0
1
4
9
16
25
36
49
64
81
100

anonymous method delegate invocation

<source lang="csharp">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);
       }
   }</source>

Anonymous Methods

<source lang="csharp">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();
   }

}</source>

Create a delegate by declaration

<source lang="csharp">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"));
   }

}</source>

Demonstrate a captured variable.

<source lang="csharp">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);  
 } 

}</source>

end:3
end:5