您的位置:首页 > 编程语言 > C#

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

2015-08-11 16:34 399 查看
原文地址:https://msdn.microsoft.com/zh-cn/library/bb882516.aspx

匿名函数是一个“内联”语句或表达式,可在需要委托类型的任何地方使用。 可以使用匿名函数来初始化命名委托,或传递命名委托(而不是命名委托类型)作为方法参数。

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

实例参考:

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 static void Main(string[] args)
{
        //Action封装一个方法,该方法只有一个参数并且不返回值。
               //更多实例见这里:https://msdn.microsoft.com/zh-cn/library/018hxwa8.aspx
var dd = new Action<string>((item) =>
{
var s = string.Concat("aa", item);
Console.Write(s.ToString());
});

dd("bb");

Console.ReadKey();
}

}
}


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);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: