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给的>的内容,我们实现了使用委托来构建我们自己的消息系统的过程.但是在日常的开发中,仍然有很多开发者因为 ...
随机推荐
- TSQL Beginners Challenge 1 - Find the second highest salary for each department
很久以前准备写的系列文章,后来因为懒一直耽搁着,今天突然决定继续下去,于是有了这篇文章,很基础,但很常用.题目描述依然拷贝.简单来说就是找出个个部门薪水排名第二的人,排名相同的要一起列出来. Intr ...
- C# 通过hessian调Java注意事项
照理说C#可以通过标准的web服务可以轻松地调用Java,但是鉴于hessian的高性能及开发效率,个人认为C#通过hessian调用java是很值得提倡的.之前完成的一个比较大型的企业应用项目就是采 ...
- oracle安装遇到的问题
这两天要做一个项目,教师招聘系统.要用oracle.就安装了oracle 12c,安装的过程中遇到了一些问题,最后自己解决了.我是win7系统. 第一个报错: [INS-30131]执行安装程序验证所 ...
- java 计算器SWT/RAP(版本3)键盘鼠标兼容
java 计算器SWT/RAP(版本3)键盘鼠标兼容,之前版本也对,但存在线程失效问题,当多人访问时,就容易线程失效,一直犯得一个错误就是一直用static变量和static方法, 之前加了什么js界 ...
- 『重构--改善既有代码的设计』读书笔记----Extract Class
在面向对象中,对于类这个概念我们应该有一个清晰的责任认识,就是每个类应该只有一个变化点,每个类的变化应该只受到单一的因素,即每个类应该只有一个明确的责任.当然了,说时容易做时难,很多人可能都会和我一样 ...
- 如何参与github上的开源项目
今晚比较闲,于是乎装修了一下博客,顺便将一块心病(怎么参加github上的开源项目)解决了,最后发个文章总结下 这些是参考的链接 http://blog.csdn.net/five3/article/ ...
- JQUERY1.9学习笔记 之可见性过滤器(二) 可见选择器
描述:选择所有可见的元素. 例:点击时让所有的可见的div元素变黄. <!doctype html><html lang="en"> <head> ...
- PHP获取客户端和服务器端IP
客户端的ip变量: $_SERVER['REMOTE_ADDR'] :客户端IP,也有可能是代理IP $_SERVER['HTTP_CLIENT_IP']:代理端的IP,可能存在,也可能伪造 $_SE ...
- php获取https下的内容
直接用file_get_contents,会报错: $url = ('https://xxx.com"); file_get_contents($url); 错误: Warning: fil ...
- python自动开发之第十二天
一.数据库的介绍 (1)由多张表组成(2)存取有规则,数据有关联(3)数据量大,被优化 好处:更有效的存取数据 二.关系型数据库管理系统(RDBMS) Oracle,Mysql,Sqlserver,D ...