原文地址: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委托的更多相关文章

  1. 无法将 匿名方法 转换为类型“System.Delegate”,因为它不是委托类型:解决方法

    http://blog.csdn.net/xiaochongchong1248/archive/2009/11/20/4841193.aspx?1271573283 编程环境要求:VS2008/FX2 ...

  2. 关于委托:异常{ 无法将 匿名方法 转换为类型“System.Delegate”,因为它不是委托类型 }

    转自:http://www.cnblogs.com/xiaofei59/archive/2010/11/25/1887285.html 异常{ 无法将 匿名方法 转换为类型“System.Delega ...

  3. 关于委托:异常{ 无法将 匿名方法 转换为类型“System.Delegate”,因为它不是委托类型 }

    异常{ 无法将 匿名方法 转换为类型"System.Delegate",因为它不是委托类型 } 委托实际上是把方法名作为参数,但是若有好多个方法时,就要指明是哪个参数  查看如下代 ...

  4. 用五分钟重温委托,匿名方法,Lambda,泛型委托,表达式树

    这些对老一代的程序员都是老生常谈的东西,没什么新意,对新生代的程序员却充满着魅力.曾经新生代,好多都经过漫长的学习,理解,实践才能掌握委托,表达式树这些应用.今天我尝试用简单的方法叙述一下,让大家在五 ...

  5. 转帖:用五分钟重温委托,匿名方法,Lambda,泛型委托,表达式树

    用五分钟重温委托,匿名方法,Lambda,泛型委托,表达式树 这些对老一代的程序员都是老生常谈的东西,没什么新意,对新生代的程序员却充满着魅力.曾经新生代,好多都经过漫长的学习,理解,实践才能掌握委托 ...

  6. 委托,匿名方法,Lambda,泛型委托,表达式树

    一.委托:完成一个委托应分三个步骤://step01:首先用delegate定义一个委托;public delegate int CalculatorAdd(int x, int y);//step0 ...

  7. C#委托,匿名方法,Lambda,泛型委托,表达式树代码示例

    第一分钟:委托 有些教材,博客说到委托都会提到事件,虽然事件是委托的一个实例,但是为了理解起来更简单,今天只谈委托不谈事件.先上一段代码: 下边的代码,完成了一个委托应用的演示.一个委托分三个步骤: ...

  8. 转载: jQuery事件委托( bind() \ live() \ delegate()) [委托 和 绑定的故事]

    转载:http://blog.csdn.net/zc2087/article/details/7287429 随着DOM结构的复杂化和Ajax等动态脚本技术的运用,事件委托自然浮出了水面.jQuery ...

  9. 匿名方法、Lambda表达和自定义泛型委托以及Func、Action系统泛型委托

    1.匿名方法的概念:一个方法没有具体的名称,而只有关键字delegate.方法参数.方法体.这种方法是匿名方法. 匿名方法的好处:将具体方法和委托直接关联在一起,如果我们基于委托只需要一个方法的时候, ...

  10. 匹夫细说C#:委托的简化语法,聊聊匿名方法和闭包

    0x00 前言 通过上一篇博客<匹夫细说C#:庖丁解牛聊委托,那些编译器藏的和U3D给的>的内容,我们实现了使用委托来构建我们自己的消息系统的过程.但是在日常的开发中,仍然有很多开发者因为 ...

随机推荐

  1. oracle - 创建数据库

    在服务器端的oracle,用户有点差异,当 我通过 'sqlplus / as sysdba' 命令登陆后,并不能创建数据库,sysdba拥有最高的系统权限,登陆后是 sys,以as sysdba登录 ...

  2. Two ways to create file using 'doc'.

    Here are two ways to create file using 'doc' command: Method i: copy con [file name][enter key] [con ...

  3. OC - 25.CAKeyframeAnimation

    概述 简介 CAKeyframeAnimation又称关键帧动画 CAKeyframeAnimation是抽象类CAPropertyAnimation的子类,可以直接使用 通过values与path两 ...

  4. javascript DOM小结

    一:定义 dom:文档对象模型. dom是针对HTML和XML文档的一个API.dom描绘了一个层次化的节点树,允许开发人员添加.移除.修改页面的某一部分. 1:childNodes(返回当前节点的子 ...

  5. 段落排版--对齐(text-aliagn)

    想为块状元素中的文本.图片设置居中样式吗?可以使用text-align样式代码,如下代码可实现文本居中显示.(那么什么是块状元素呢?后面会讲到呢~) h1{ text-align:center; } ...

  6. 九、C# 合式类型

    本章要描述如何最终完善类型声明.   1.重写Ojbect中的成员   重写ToString() 默认情况下,在任何对象上调用 ToString()会返回类的完全限定名称,所以有时候需要重载这个函数, ...

  7. java.lang.reflection打印一个类的全部信息

    package com.ljy.chapter5; import java.lang.reflect.Constructor; import java.lang.reflect.Field; impo ...

  8. 【Ural1057】幂和的数量

    [题目描述] 写一个程序来计算区间[X,Y]内满足如下条件的整数个数:它恰好等于K个互不相等的B的整数幂之和. 举个例子.令X=15,Y=20,K=2,B=2.在这个例子中,区间[15,20]内有3个 ...

  9. seaJs初体验

    目录结构 模块定义define define(function(require,exports,module){ //exports可以把方法或属性暴露给外部 exports.name = 'hell ...

  10. yii 验证确认密码是否一致 【"compare",'compareAttribute'=>'password'】

    array("surepassword","compare",'compareAttribute'=>'password','message'=>' ...