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. 用原生css实现高斯模糊、黑白等滤镜效果

    —引导— 在CSS3中,有一个强大的属性,那就是filter属性,filter顾名思义就是“滤镜”的意思,用filter属性可以让图片无需PS处理就达到一些简单的显示效果. —定义和使用— filte ...

  2. NET上传大文件出现网页无法显示的问题 默认的上传文件大小是4M

    需要在配置文件处进行修改 web.config中的<system.web></system.web>内加入如下代码: <httpRuntime executionTime ...

  3. It企业的上市与退市

    目前我国的it上市公司有同方股份.华胜天成.长城电脑.航天信息.用友软件.中国软件.东软集团.长电科技.华东科技.航天长峰.航天科技.士兰微.上海贝岭等等. 一般来讲公司上市是为了融资,一是为了解决资 ...

  4. IE11下使用fixed定位时鼠标滚动不平滑

    很久不用IE了,近期做兼容性测试发现一个fixed定位的问题,当元素使用fixed定位时,其应该不随页面滚动,在chrome/firefox/edge下都很完美,元素完全不动,但是使用IE11时,如果 ...

  5. bitbucket使用教程

    Bitbucket使用说明: 使用者请直接看第一步,第二步和egit使用说明, 需要自己创建仓库的可以看三四步 第一步:新用户注册 bitbucket.org 然后按步骤创建一个教程代码库 可以选择下 ...

  6. golang subprocess tests

    golang Subprocess tests Sometimes you need to test the behavior of a process, not just a function. f ...

  7. L - Ch’s gift HDU - 6162

    Ch’s gift Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total S ...

  8. Guideline 2.1 - Information Needed需要补充录制视频

    1.被拒回文 Guideline 2.1 - Information Needed We have started the review of your app, but we are not abl ...

  9. avascript的匿名函数

    一.什么是匿名函数? 在Javascript定义一个函数一般有如下三种方式: 函数关键字(function)语句: function fnMethodName(x){alert(x);} 函数字面量( ...

  10. SHELL编程之条件测试

    条件测试 (一)概念:对特定的条件进行判断,以决定如何执行操作,当条件成立时,测试语句的返回值为0,否则为其他数值,意思就是如果 echo $? 的值是0,那么条件成立.条件测试的分类:文件测试.整数 ...