1.使用简单委托

namespace 简单委托
{
class Program
{
//委托方法签名
delegate void MyBookDel(int a);
//定义委托
static MyBookDel myBookDel;
//普通方法
public static void MathBook(int a)
{
Console.WriteLine("我是数学书" + a);
}
static void Main(string[] args)
{
myBookDel += MathBook;
myBookDel();
Console.ReadKey();
}
}
}

2.Action委托

/// <summary>
/// Action委托的优势是在于不用先去定义 方法签名 然后再用,可以直接使用
/// 他把他所支持的参数类型写在了<>里面,所以不需要单独去定义方法签名了
/// 不过Action不支持返回值
/// </summary>
namespace Action委托
{
class Program
{
static Action<int> ac;
public static void MathBook(int a)
{
Console.WriteLine("数学" + a);
}
static void Main(string[] args)
{
ac += MathBook;
ac();
Console.ReadKey();
}
}
}

3.Func委托

/// <summary>
/// Func委托几乎和Action一样,唯一的区别就是Func支持返回值
/// Func的<>里面的前面的是参数,最后一个值永远代表返回值的类型
/// </summary>
namespace Func委托
{
class Program
{
static Func<int, string> func;
public static string MyBook(int a)
{
return "哈哈哈" + a;
}
static void Main(string[] args)
{
func += MyBook;
Console.WriteLine(func());
Console.ReadKey();
}
}
}

4.匿名方法

/// <summary>
/// 匿名方法就是写委托时,不想再单独定义一个方法体,然后再进行+= 的一步操作,直接把方法体写在+= 的后面,这种方法就叫匿名方法
/// </summary>
namespace 匿名方法
{
class Program
{
delegate int NiMingDel(int a, int b);
static NiMingDel nimingDel;
static void Main(string[] args)
{
nimingDel += delegate (int x, int y) { return x + y; };
//计算1 + 2
Console.WriteLine(nimingDel(, ));
Console.ReadKey();
}
}
}

5.lambda表达式

/// <summary>
/// 匿名方法用lambda表达式去代替
/// = (参数) => {方法体}
/// </summary>
namespace 合体
{
class Program
{
delegate int NiMingDel(int a, int b);
static NiMingDel nimingDel;
static void Main(string[] args)
{
//一种语法糖
nimingDel = (x, y) => x + y;
//计算1 + 2
Console.WriteLine(nimingDel(, ));
Console.ReadKey();
}
}
}

6.合体

/// <summary>
/// Func<> 与 lambda表达式的合体
/// </summary>
namespace _6.合体
{
class Program
{
static void Main(string[] args)
{
Func<int, int, string> func = (x, y) => { return "计算结果是:" + (x + y); };
Console.WriteLine(func(, ));
Console.WriteLine("c#牛逼!");
Console.ReadKey();
}
}
}

委托delegate 泛型委托action<> 返回值泛型委托Func<> 匿名方法 lambda表达式 的理解的更多相关文章

  1. C# delegate event func action 匿名方法 lambda表达式

    delegate event action func 匿名方法 lambda表达式 delegate类似c++的函数指针,但是是类型安全的,可以指向多个函数, public delegate void ...

  2. 委托-异步调用-泛型委托-匿名方法-Lambda表达式-事件【转】

    1. 委托 From: http://www.cnblogs.com/daxnet/archive/2008/11/08/1687014.html 类是对象的抽象,而委托则可以看成是函数的抽象.一个委 ...

  3. C#多线程+委托+匿名方法+Lambda表达式

    线程 下面是百度写的: 定义英文:Thread每个正在系统上运行的程序都是一个进程.每个进程包含一到多个线程.进程也可能是整个程序或者是部分程序的动态执行.线程是一组指令的集合,或者是程序的特殊段,它 ...

  4. C#委托总结-匿名方法&Lambda表达式

    1,匿名方法 匿名方法可以在声明委托变量时初始化表达式,语法如下 之前写过这么一段代码: delegate void MyDel(string value); class Program { void ...

  5. Delegate,Action,Func,匿名方法,匿名委托,事件 (转载)

    Delegate,Action,Func,匿名方法,匿名委托,事件 (转载) 一.委托Delegate 一般的方法(Method)中,我们的参数总是string,int,DateTime...这些基本 ...

  6. .NET Framework System.Array.Sort 数组类,加深对 IComparer、IComparable 以及泛型委托、匿名方法、Lambda 表达式的理解

    本文内容 自定义类 Array.Sort 参考资料 System.Array.Sort 有很多对集合的操作,比如排序,查找,克隆等等,你可以利用这个类加深对 IComparer.IComparable ...

  7. 对比两个同类型的泛型集合并返回差异泛型集合 ——两个List<类名>的比较

    1: /// <summary> 2: /// 对比两个同类型的泛型集合并返回差异泛型集合 3: /// </summary> 4: /// <typeparam nam ...

  8. JsonResult作为Action返回值时的错误

    JsonResult作为Action返回值时的错误   System.InvalidOperationException: This request has been blocked because ...

  9. Controller 中Action 返回值类型 及其 页面跳转的用法

        •Controller 中Action 返回值类型 View – 返回  ViewResult,相当于返回一个View 页面. -------------------------------- ...

随机推荐

  1. 【C#】is 和 as

    看个例子: public class User { } public class Group { } class Program { static void Main(string[] args) { ...

  2. whisper简介

    以太坊系列之二十 以太坊中的基础应用whisper 以太坊系列之二十 以太坊中的基础应用whisper 1 whisper介绍 2 whisper rpc模块 3 whisper中的消息 4 消息的加 ...

  3. 001.linux的基础优化(期中架构方面的优化)

    1. linux内核优化 第一步 cat >>/etc/sysctl.conf<<EOF net.ipv4.tcp_fin_timeout = 2 net.ipv4.tcp_t ...

  4. linux系统使用sh文件传参数给matlab程序

      linux系统下使用sh文件传参数给matlab程序 (1)编写sh文件 程序以下面的行开始(必须在文件的第一行):   #!/bin/sh 定义需要传递的参数,用双引号引起,参数之间使用逗号或分 ...

  5. 洛谷P4526 【模板】自适应辛普森法2(Simpson法)

    题面 传送门 题解 据说这函数在\(x>15\)的时候趋近于\(0\) 据说当且仅当\(a<0\)时积分发散 所以直接套自适应\(simpson\)吧-- //minamoto #incl ...

  6. postgreSQL PL/SQL编程学习笔记(一)

    1.Structure of PL/pgSQL The structure of PL/pgSQL is like below: [ <<label>> ] [ DECLARE ...

  7. 从pg_hba.conf文件谈谈postgresql的连接认证

    最近一直在弄postgresql的东西,搭建postgresql数据库集群环境什么的.操作数据库少不得要从远程主机访问数据库环境,例如数据库管理员的远程管理数据库,远程的客户存取数据库文件. 而在po ...

  8. php中的openssl开启方法

    windows下开启方法: 1: 首先检查php.ini中:extension=php_openssl.dll是否存在, 如果存在的话去掉前面的注释符‘:', 如果不存在这行,那么添加extensio ...

  9. Sql server inner join......on

    --查询的时候,如果表中有重名的列,此时,应该在通过 表名.列名 的方式来限定指定的列是哪张表中的.select PhoneNum.pid, PhoneNum.pname, PhoneNum.pcel ...

  10. [POI2007]MEG-Megalopolis 树的dfs序+树状数组维护差分 BZOJ1103

    题目描述 Byteotia has been eventually touched by globalisation, and so has Byteasar the Postman, who onc ...