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 表达式取代了匿名方法,作为编写内联代码的首选方式。
实例参考:
多线程操作
匿名函数是一个“内联”语句或表达式,可在需要委托类型的任何地方使用。 可以使用匿名函数来初始化命名委托,或传递命名委托(而不是命名委托类型)作为方法参数。
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); } }
相关文章推荐
- C# ListView用法详解
- 使用未公开关键字在 C# 中导入外部 printf 等参数数量可变函数 [2] C# 实现
- 使用未公开关键字在 C# 中导入外部 printf 等参数数量可变函数 [1] CLR 支持
- C#中的虚函数的作用
- c# 获取IP
- C# in depth ( 第五章 进入快速通道的委托)
- C#操作EXCEL时,判断一个单元格是否为空的方式
- Visual C#2010学习笔记五之变量和常量的使用
- C# 学习笔记
- C#中的枚举类型
- C#操作目录和文件讲解
- C#Winform常引用的库
- C#中StreamReader读取中文时出现乱码问题总结
- C# 大文本的读取处理
- C#定点任务代码 类似Windows计划任务(健壮性高)
- 学习c#中遇到的一些问题(2)
- C#实现将图片另存到新建的文件夹中
- C# 基础小知识之yield 关键字 语法糖
- C#程序中将图片转换为byte数组,并将byte数组转换为图片
- C# Random 生成不重复随机数