【委托】是一个类
可以把一个方法当作另一个方法的参数使用。

声明委托:delegate string 委托名(参数列表);//跟定义方法一样,只是没有方法体,必须使用关键字delegate
使用委托的函数 返回值和参数列表与委托相同【必须记住】
使用委托: 委托名 委托变量名=new 委托(函数名); 委托变量名(参数列表);

         public ActionResult Index()
{
de1 de = new de1(EN);
string str = de("liuph", );//str="liuph is 1 years old"
de1 de1 = CN;
string str1 = de1("liuph", );//str="liuph的年龄是1岁"
return View();
}
delegate string de1(string name, int age);//定义
static string CN(string name, int age)
{
return name + "的年龄是" + age + "岁";
}
static string EN(string name, int age)
{
return name + " is " + age + " years old";
}

=========

多播委托
包含多个方法的委托;按顺序调用,返回值必须void(否则返回最后调用方法的值);其中一个发生异常,不再继续向下执行;

         public ActionResult Index()
{
de1 de = new de1(CN);//使用
de += EN;//连续调用第二个
string str = de("liuph", );//str="liuph is 1 years old "
return View();
}
delegate string de1(string name, int age);//定义
static string CN(string name, int age)
{
return name + "的年龄是" + age + "岁";
}
static string EN(string name, int age)
{
return name + " is " + age + " years old";
}
         static string str = "";
public ActionResult Index()
{
de1 de = CN;
de += EN;
de("liuph", );
//str="liuph的年龄是1岁liuph is 1 years old ";
return View();
}
delegate void de1(string name, int age);
static void CN(string name, int age)
{
str += name + "的年龄是" + age + "岁";
}
static void EN(string name, int age)
{
str += name + " is " + age + " years old";
}

发生异常也向下执行 使用GetInvocationList()方法

         public ActionResult Index()
{
de1 de = new de1(CN);//使用
de += EN;//连续调用第二个
Delegate[] des = de.GetInvocationList();
string str = "";
foreach (de1 item in des)//循环
{
try
{
str += item("liu", );
}
catch (Exception)
{
str += "有错误";
}
}
//EN注释throw 结果 str="liu的年龄是1岁liu is 1 years old "
//EN注释return 结果 str="liu的年龄是1岁有错误 "
return View();
}
delegate string de1(string name, int age);
static string CN(string name, int age)
{
return name + "的年龄是" + age + "岁";
}
static string EN(string name, int age)
{
throw new Exception("");
return name + " is " + age + " years old";
}

==========
匿名方法的委托

 public ActionResult Index()
{
de1 de = delegate(string name, int age)
{
return name + "的年龄是" + age + "岁";
};
string str = de("liuph", );//str="liuph的年龄是1岁";
return View();
}
delegate string de1(string name, int age);

==========
泛型Action<T>委托表示引用一个void返回类型的方法

         static string str = "";
public ActionResult Index()
{
Action<int> action;
action = de;
de();
//str="liuph的年龄是1岁 ";
return View();
}
public static void de(int i)
{
str = string.Format("liuph的年龄是{0}岁", i);
}

泛型Func<in T,out TResult>允许调用带返回类型的方法

 public ActionResult Index()
{
Func<int,string> action;
action = de;
string str = de();//str="liuph的年龄是1岁 ";
return View();
}
public static string de(int i)
{
return string.Format("liuph的年龄是{0}岁", i);
}

【Lambda】(个人理解就是匿名委托的简单化)
运算符=>,左边是参数,多个参数用圆括号;右边是实现代码

         public ActionResult Index()
{
del myDelegate = x => x * x;
int j = myDelegate(); //j = 25
Func<int, string, string> de = (x, y) => qq(x, y);
string str = de(, "你输入的数字是:");//str="你输入的数字是:12" Func<string, string, string> de1 = (x, y) => { return x + y; };
string str1 = de1("haha", "哈哈");//str1="haha哈哈"
return View();
}
delegate int del(int i);
public static string qq(int i, string str)
{
return str + i;
}

Lambda语句
就是把Lambda表达式写在花括号中

Lambda可以使用外部变量(慎用)
例如:
int y = 12;
del myDelegate = x => x * y;//想获取12*x必须保证使用委托时y值不会变
y = 13;
int j = myDelegate(5); //j = 65

C# 委托和Lambda---基础的更多相关文章

  1. 系统预定义委托与Lambda表达式

    NET中那些所谓的新语法之三:系统预定义委托与Lambda表达式   开篇:在上一篇中,我们了解了匿名类.匿名方法与扩展方法等所谓的新语法,这一篇我们继续征程,看看系统预定义委托(Action/Fun ...

  2. C# Note2:委托(delegate) & Lambda表达式 & 事件(event)

    前言 本文主要讲述委托和Lambda表达式的基础知识,以及如何通过Lambda表达式实现委托调用,并阐述.NET如何将委托用作实现事件的方式. 参考:C#高级编程 1.什么是委托(delegate)? ...

  3. 委托学习过程及委托、Lambda表达式和匿名方法的关系总结及事件总结

    第一章,当开始学习委托的时候,我们会问什么是委托?为什么要学习委托? 一,什么是委托? 委托是一个类,它定义了方法的类型,使得可以将方法当作另一个方法的参数来进行传递,这种将方法动态地赋给参数的做法, ...

  4. C#从委托、lambda表达式到linq总结

    前言 本文总结学习C#必须知道的基础知识,委托.监视者模式.常用lambda表达式.linq查询,自定义扩展方法,他们之间有什么关系呢?匿名委托是如何演变成lambda表达式,lambda再如何导出l ...

  5. 委托、匿名委托、Lambda 表达式、Expression表达式树之刨根问底

    本篇不是对标题所述之概念的入门文章,重点在阐述它们的异同点和应用场景.各位看官,这里就不啰嗦了,直接上代码. 首先定义一个泛型委托类型,如下: public delegate T Function&l ...

  6. 使用匿名委托,Lambda简化多线程代码

    使用匿名委托,Lambda简化多线程代码   .net中的线程也接触不少了.在多线程中最常见的应用莫过于有一个耗时的操作需要放到线程中去操作,而在这个线程中我们需要更新UI,这个时候就要创建一个委托了 ...

  7. 转载 C#匿名函数 委托和Lambda表达式

    转载原出处: http://blog.csdn.net/honantic/article/details/46331875 匿名函数 匿名函数(Anonymous Function)是表示“内联”方法 ...

  8. 十二、C# 委托与Lambda表达式(匿名方法的另一种写法)

    委托与Lambda表达式   1.委托概述 2.匿名方法 3.语句Lambda 4.表达式Lambda 5.表达式树   一.委托概述 相当于C++当中的方法指针,在C#中使用delegate 委托来 ...

  9. 委托与Lambda表达式

    ~,先不急说委托和Lambda表达式,先看两个例子再说: 1. 通过委托,为一个数字加10,如下代码: class Program { private delegate int JiSuan(int ...

  10. C#函数式程序设计之函数、委托和Lambda表达式

    C#函数式程序设计之函数.委托和Lambda表达式 C#函数式程序设计之函数.委托和Lambda表达式   相信很多人都听说过函数式编程,提到函数式程序设计,脑海里涌现出来更多的是Lisp.Haske ...

随机推荐

  1. poj 2104:K-th Number(划分树,经典题)

    K-th Number Time Limit: 20000MS   Memory Limit: 65536K Total Submissions: 35653   Accepted: 11382 Ca ...

  2. Could not link against boost_system 解决办法

    Could not link against boost_system 解决办法: 先安装 libboost-all-dev ./configure --with-incompatible-bdb - ...

  3. 浅谈config文件的使用

    一.缘起 最近做项目开始使用C#,因为以前一直使用的是C++,因此面向对象思想方面的知识还是比较全面的,反而是因没有经过完整.系统的.Net方面知识的系统学习,经常被一些在C#老鸟眼里几乎是常识的小知 ...

  4. android 入门-R文件的死与活

    1.图片的名字Btn_Play R文件死了. 1.答:修改图片的名字btn_play R文件活了.

  5. wp8 入门到精通 定时更新瓷贴

    public class ScheduledAgent : ScheduledTaskAgent { static ScheduledAgent() { Deployment.Current.Disp ...

  6. PHPCMS_v9 wap不同列表采用不同模板的方法

    .在phpcms\modules\wap\index.php中搜索 $template = ($TYPE[$typeid]['parentid']==0 && in_array($ty ...

  7. [Linux] 解压tar.gz文件,解压部分文件

    遇到数据库无法查找问题原因,只能找日志,查找日志的时候发现老的日志都被压缩了,只能尝试解压了   数据量比较大,只能在生产解压了,再进行查找 文件名为*.tar.gz,自己博客以前记录过解压方法: h ...

  8. 在ASP.NET 5项目中使用和调试外部源代码包

    (此文章同时发表在本人微信公众号"dotNET每日精华文章",欢迎右边二维码来关注.) 题记:由于在ASP.NET 5中,项目依赖都是通过"包"来引用,所以使用 ...

  9. WPF之MVVM(Step3)——使用Prism(1)

    使用WPF-MVVM开发时,自己实现通知接口.DelegateCommand相对来说还是用的较少,我们更多的是使用第三方的MVVM框架,其中微软自身团队提供的就有Prism框架,此框架功能较多,本人现 ...

  10. VR 相关专业词汇

    最近在看 SIGGRAPH2015 有关 VR Display and Interaction 的几篇文章,之前从来没看过有关方面的 paper,一看才发现专业词汇太多了,根本不懂啊,幸亏 Paper ...