C# 匿名方法 委托 Action委托 Delegate委托
原文地址: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 > ; DelegateAge delegateAge2 = (age) =>
{
return age > ;
}; Console.WriteLine(delegateAge1());
Console.WriteLine(delegateAge2()); Console.ReadKey();
} }
}
using System; namespace ConsoleTest
{
internal class Program
{
private delegate void Del();
private static void Main(string[] args)
{ int n = ;
//没有参数的情况下可以这么玩
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(, );
d2(, );
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(); // 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# 匿名方法 委托 Action委托 Delegate委托的更多相关文章
- 无法将 匿名方法 转换为类型“System.Delegate”,因为它不是委托类型:解决方法
http://blog.csdn.net/xiaochongchong1248/archive/2009/11/20/4841193.aspx?1271573283 编程环境要求:VS2008/FX2 ...
- 关于委托:异常{ 无法将 匿名方法 转换为类型“System.Delegate”,因为它不是委托类型 }
转自:http://www.cnblogs.com/xiaofei59/archive/2010/11/25/1887285.html 异常{ 无法将 匿名方法 转换为类型“System.Delega ...
- 关于委托:异常{ 无法将 匿名方法 转换为类型“System.Delegate”,因为它不是委托类型 }
异常{ 无法将 匿名方法 转换为类型"System.Delegate",因为它不是委托类型 } 委托实际上是把方法名作为参数,但是若有好多个方法时,就要指明是哪个参数 查看如下代 ...
- 用五分钟重温委托,匿名方法,Lambda,泛型委托,表达式树
这些对老一代的程序员都是老生常谈的东西,没什么新意,对新生代的程序员却充满着魅力.曾经新生代,好多都经过漫长的学习,理解,实践才能掌握委托,表达式树这些应用.今天我尝试用简单的方法叙述一下,让大家在五 ...
- 转帖:用五分钟重温委托,匿名方法,Lambda,泛型委托,表达式树
用五分钟重温委托,匿名方法,Lambda,泛型委托,表达式树 这些对老一代的程序员都是老生常谈的东西,没什么新意,对新生代的程序员却充满着魅力.曾经新生代,好多都经过漫长的学习,理解,实践才能掌握委托 ...
- 委托,匿名方法,Lambda,泛型委托,表达式树
一.委托:完成一个委托应分三个步骤://step01:首先用delegate定义一个委托;public delegate int CalculatorAdd(int x, int y);//step0 ...
- C#委托,匿名方法,Lambda,泛型委托,表达式树代码示例
第一分钟:委托 有些教材,博客说到委托都会提到事件,虽然事件是委托的一个实例,但是为了理解起来更简单,今天只谈委托不谈事件.先上一段代码: 下边的代码,完成了一个委托应用的演示.一个委托分三个步骤: ...
- 转载: jQuery事件委托( bind() \ live() \ delegate()) [委托 和 绑定的故事]
转载:http://blog.csdn.net/zc2087/article/details/7287429 随着DOM结构的复杂化和Ajax等动态脚本技术的运用,事件委托自然浮出了水面.jQuery ...
- 匿名方法、Lambda表达和自定义泛型委托以及Func、Action系统泛型委托
1.匿名方法的概念:一个方法没有具体的名称,而只有关键字delegate.方法参数.方法体.这种方法是匿名方法. 匿名方法的好处:将具体方法和委托直接关联在一起,如果我们基于委托只需要一个方法的时候, ...
- 匹夫细说C#:委托的简化语法,聊聊匿名方法和闭包
0x00 前言 通过上一篇博客<匹夫细说C#:庖丁解牛聊委托,那些编译器藏的和U3D给的>的内容,我们实现了使用委托来构建我们自己的消息系统的过程.但是在日常的开发中,仍然有很多开发者因为 ...
随机推荐
- PHP 解决时差8小时的问题
有时候用php echo date("Y-m-d H:i:s")的时候会发现自己的时间和系统时间有差别 这里问题一般就是因为你自己的时区和配置的时区出现了差别的原因: 解决办法有三 ...
- webGIS(离线版)研究路线归总
特注:不做详解,说明网上资源很多,找一篇,照着走一遍即可. 1.数据源要满足开源.Free且地理信息要完善 几经周折,选择了OSM,具体信息可以去其官方查看(它竟然把中国一分为二,大陆.台湾,坑爹!! ...
- 利用bat批量执行脚本文件
1.读取目录文件 利用bat 的for命令读取中的sql文件 for /r %%c in (0*.sql) do echo %%c %%c 相当于变量 in() 中的为循环的范围 此句的作用是显示当前 ...
- JAVA除去制定字符的方法
只需调用replaceAll()方法: public class Test { public static void main(String[] args) { String s= "abc ...
- xcode本地运行H5游戏可以吗?
答案是不可以!!! 不可以!!! 不可以!!! 有时候很郁闷的接受一个需求,然后以为自己能力不行,或者是代码写错,还是哪里了解不够,然而并不是啊!!! MD的我想说有些事是行不通的的!! 好了,平静下 ...
- Objective-C和C++的区别
1.都是有C语言延伸而来2.OC是完全动态的,C++是部分动态的3.OC不支持多继承,通过代理 类别 协议优雅的实现了相关的一系列特性4.调用机制不同OC里面叫发送消息 C++叫做调用函数数5.OC ...
- 【vc】14_网络编程_socket编程
1.计算机网络基本知识 最简单的网络程序如图: 提示:IP地址就相当于一个公司的总机号码,端口号就相当于分机号码.在打电话时,拨通总机后,还需要转到分机上. (1)协议 ·为进行网络中的数据交换(通信 ...
- SGU 128.Snake
时间限制:0.25s 空间限制:4m 题意: 在一个平面坐标中有N个点,现在要你用这N个点构造一个闭合图形,这个图形要满足以下条件: 1.这个图形要是闭合的: 2.图形上的点只能是给 ...
- 简单高效读写修改整个文本Slurp
语法: use File::Slurp; #标量环境下一次读取所有文本内容到标量中. my $text = read_file( 'filename' ) ; # 读取文本的所有行到数组中. my ...
- cocos2d-x编译错误问题
在xcode中创建的cocos2d-x项目,然后添加了一个基类,里面有虚方法,编译时出错,错误如下: Undefined symbols for architecture x86_64: " ...