委托 匿名方法 Lambda表达式 总结(yuananl)

委托 匿名方法 Lambda表达式 总结(yuananl)

委托是一种引用方法的类型。一旦为委托分配了方法,委托将与该方法具有完全相同的行为。委托方法的使用可以
像其他任何方法一样,具有参数和返回值。委托就是给一个函数起一个别的名字。请看下面的代码:

using System;//一般情况下系统会自动引入该命名空间
漫画
// Declare delegate -- defines required signature:
delegate void SampleDelegate(string message);//声明一委托类型

class MainClass
{
     // Regular method that matches signature:
     static void SampleDelegateMethod(string message)//一静态方法
     {
         Console.WriteLine(message);
     }

     static void Main()
     {
         // Instantiate delegate with named method:用一个已经声明的方法初始化一委托对象
         SampleDelegate d1 = SampleDelegateMethod;//声明一委托对象并为之挂接一方法
         // Instantiate delegate with anonymous method用一个匿名方法来初始化一委托对象
         SampleDelegate d2 = delegate(string message)//
         {
             Console.WriteLine(message);
         };

         d1("Hello");output:Hello
        
         d2("World,http://www.dc9.cn/");output:World,http://www.dc9.cn/

     }
}

下面的是一事件(特殊类型的委托)

public Form1()
{
     InitializeComponent();

      this.Click += this.MultiHandler;//窗体的单击事件挂接下面的一事件
}

private void MultiHandler(object sender, System.EventArgs e)
{
     MessageBox.Show(((MouseEventArgs)e).Location.ToString());
}


匿名方法

匿名方法就是没名儿的委托。虽然没名,但是必须加”delegate“来表示我没名。

// Create a delegate instance
delegate void Del(int x);

// Instantiate the delegate using an anonymous method
Del d = delegate(int k) { /* ... */ };等价于

Del d =delegate(int k)

{ 语句 }

Del d = delegate() { System.Console.WriteLine("Copy #:{0}", ++n); };

delegate void Printer(string s);//又一索引(其参数为string类型)

Printer p = delegate(string j)//匿名方法:即P所关联的方法(匿名)
         {
             System.Console.WriteLine(j);
         };

p("The delegate using the anonymous method is called.");

delegate string Printer(string s);
         private void button1_Click(object sender, EventArgs e)
         {

                      Printer yuanl =delegate(string j)
                     {
                        return (j)+"烦死了";
                     };

             Console.WriteLine(    yuanl("The delegate using the anonymous method is called.")    );


         }

delegate void Printer(string s);
         private void button1_Click(object sender, EventArgs e)
         {
             Printer p = new Printer(Form1.DoWork);
             p("http://www.dc9.cn/");
         }
         static void DoWork(string k)
         {
             System.Console.WriteLine(k);
         }

漫画

  使用Lambda表达式可以这么写: 强

delegate string Printer(string s);
         private void button1_Click(object sender, EventArgs e)
         {

             Printer yuanl = j => j+"烦死了!!!";

             Console.WriteLine(yuanl("The delegate using the anonymous method is called."));


         }

public Form1()
{
     InitializeComponent();

      this.Click += (s, e) => { MessageBox.Show(((MouseEventArgs)e).Location.ToString());};
}
注意(s,e)的含义:S事件的发送者,e承载传递的数据


Where是Enumerable的一个方法。3.5才有的。里面的参数是Func<(Of <(T, TResult>)>) 泛型委托

Func<(Of <(T, TResult>)>) 泛型委托

//下面的代码是一典型的委托应用

using System;

delegate string ConvertMethod(string inString);

public class DelegateExample
{
    public static void Main()
    {
     
       ConvertMethod convertMeth = UppercaseString;
       string name = "Dakota";
       Console.WriteLine(convertMeth(name));
    }

    private static string UppercaseString(string inputString)
    {
       return inputString.ToUpper();
    }
}


写成泛型委托是:

using System;
public class GenericFunc
{
    public static void Main()
    {
     
       Func<string, string> convertMethod = UppercaseString;
       string name = "Dakota";
       Console.WriteLine(convertMethod(name));
    }

    private static string UppercaseString(string inputString)
    {
       return inputString.ToUpper();
    }
}


lambda应用

        delegate bool TestFunc(string fruit);

         private void button1_Click(object sender, EventArgs e)
         {

             List<string> fruits =new List<string> { "apple", "http://www.dc9.cn", "banana", "mango",
                     "orange", "blueberry", "grape", "strawberry" };
             TestFunc f = new TestFunc(DoWork);
             Func<string, bool> f2 = DoWork;
             IEnumerable<string> query = fruits.Where(f2);//典型委托
             foreach (string fruit in query)
             Console.WriteLine(fruit);
        }

         private static bool DoWork(string k)
         {
           return    k.Length < 6;
        }

     等价于下面:

         delegate bool TestFunc(string fruit);
         private void button1_Click(object sender, EventArgs e)
         {

             List<string> fruits =new List<string> { "apple", "passionfruit", "banana", "mango",
                     "orange", "blueberry", "grape", "http://www.dc9.cn" };
             TestFunc f = DoWork;
             Func<string, bool> f2 =k=> k.Length < 6;//lambda表达式
             IEnumerable<string> query = fruits.Where(f2);
             foreach (string fruit in query)
             Console.WriteLine(fruit);
        }

         private static bool DoWork(string k)
         {
           return    k.Length < 6;
        }

       还等价于:
漫画

         private void button1_Click(object sender, EventArgs e)
         {

             List<string> fruits =new List<string> { "apple", "passionfruit", "banana", "mango",
                     "orange", "blueberry", "grape", "http://www.dc9.cn" };
            

             IEnumerable<string> query = fruits.Where(
                 delegate(string k){
                     return k.Length < 6;
                 }
                 );//匿名函数
             foreach (string fruit in query)
             Console.WriteLine(fruit);
        }

       还等价于:

        private void button1_Click(object sender, EventArgs e)
         {

             List<string> fruits =new List<string> { "apple", "passionfruit", "banana", "mango",
                     "orange", "blueberry", "grape", "http://www.dc9.cn" };
            

             IEnumerable<string> query = fruits.Where(k=>k.Length<6);//lambda表达式
             foreach (string fruit in query)
             Console.WriteLine(fruit);
        }

注意看下面的这几句:
       public delegate string yuanl(string  a, string b);
       string yuan ="yuanl";
       string an ="anxl";
       public static class LambdaTest

       {

           public static string yuananl(this string a, string y, yuanl  yl)

           {

               return yl(a, y);

           }

       }

Console.WriteLine(yuan.yuananl(an ,(yuan,an)=>yuan+"love"+an));

你可能感兴趣的:(lambda)