C# 匿名方法 委托 Action委托 Delegate委托

原文地址:https://msdn.microsoft.com/zh-cn/library/bb882516.aspx

可以使用匿名函数来初始化命名委托,或传递命名委托(而不是命名委托类型)作为方法参数。

C# 2.0 引入了匿名方法,而在 C# 3.0 及更高版本中,Lambda 表达式取代了匿名方法,作为编写内联代码的首选方式。

实例参考:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Runtime.CompilerServices;
 5 using System.Security.Cryptography.X509Certificates;
 6 using System.Text;
 7 using System.Threading.Tasks;
 8 
 9 namespace ConsoleTest
10 {
11     internal class Program
12     {
13         private static void Main(string[] args)
14         {
15             //Action封装一个方法,该方法只有一个参数并且不返回值。


//更多实例见这里:https://msdn.microsoft.com/zh-cn/library/018hxwa8.aspx16 var dd = new Action<string>((item) => 17 { 18 var s = string.Concat("aa", item); 19 Console.Write(s.ToString()); 20 }); 21 22 dd("bb"); 23 24 Console.ReadKey(); 25 } 26 27 28 } 29 }
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleTest
{
    internal class Program
    {
        private delegate bool DelegateAge(int age);
        private static void Main(string[] args)
        {
            //lambda 表达式写法
            DelegateAge delegateAge1 = (age) => age > 30;

            DelegateAge delegateAge2 = (age) =>
            {
                return age > 30;
            };

            Console.WriteLine(delegateAge1(35));
            Console.WriteLine(delegateAge2(15));
            
            Console.ReadKey();
        }

    }
}
using System;

namespace ConsoleTest
{
    internal class Program
    {
        private delegate void Del();
        private static void Main(string[] args)
        {

            int n = 0;
            //没有参数的情况下可以这么玩
            Del d = () =>
            {
                System.Console.WriteLine("Copy #:{0}", ++n);
            };
            d();

            Console.ReadKey();
        }
    }
}
using System;
using System.IO;

namespace ConsoleTest
{
    internal class Program
    {
        private delegate void Del(int a,int b);
        private static void Main(string[] args)
        {
            //多个参数的情况下可以这么玩
            Del d = (a,b) =>
            {
                System.Console.WriteLine("a+b="+(a+b).ToString());
            };
            //也可以这么玩
            Del d2 = delegate(int a, int b)
            {
                System.Console.WriteLine("a+b=" + (a + b).ToString());
            };
            d(4, 7);
            d2(4, 7);
            Console.ReadKey();
        }
    }
}

多线程操作

using System;
using System.Threading;

public class Work
{
    public static void Main()
    {
        // To start a thread using a shared thread procedure, use
        // the class name and method name when you create the 
        // ParameterizedThreadStart delegate. C# infers the 
        // appropriate delegate creation syntax:
        //    new ParameterizedThreadStart(Work.DoWork)
        //
        Thread newThread = new Thread(Work.DoWork);

        // Use the overload of the Start method that has a
        // parameter of type Object. You can create an object that
        // contains several pieces of data, or you can pass any 
        // reference type or value type. The following code passes
        // the integer value 42.
        //
        newThread.Start(42);

        // To start a thread using an instance method for the thread 
        // procedure, use the instance variable and method name when 
        // you create the ParameterizedThreadStart delegate. C# infers 
        // the appropriate delegate creation syntax:
        //    new ParameterizedThreadStart(w.DoMoreWork)
        //
        Work w = new Work();
        //可以这样写
        //newThread = new Thread(delegate(object data)
        //{
        //    Console.WriteLine("Instance thread procedure. Data='{0}'",
        //    data);
        //});

        //也可以这样写  调用的这个接口public Thread(ParameterizedThreadStart start);
        newThread = new Thread((data)=>
        {
            Console.WriteLine("Instance thread procedure. Data='{0}'",
            data);
        });

        // Pass an object containing data for the thread.
        //
        newThread.Start("The answer.");

        Console.ReadLine();
    }

    public static void DoWork(object data)
    {
        Console.WriteLine("Static thread procedure. Data='{0}'",
            data);
    }

    public void DoMoreWork(object data)
    {
        Console.WriteLine("Instance thread procedure. Data='{0}'",
            data);
    }
}