C# 委托和Lambda---基础
【委托】是一个类
可以把一个方法当作另一个方法的参数使用。
声明委托: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---基础的更多相关文章
- 系统预定义委托与Lambda表达式
NET中那些所谓的新语法之三:系统预定义委托与Lambda表达式 开篇:在上一篇中,我们了解了匿名类.匿名方法与扩展方法等所谓的新语法,这一篇我们继续征程,看看系统预定义委托(Action/Fun ...
- C# Note2:委托(delegate) & Lambda表达式 & 事件(event)
前言 本文主要讲述委托和Lambda表达式的基础知识,以及如何通过Lambda表达式实现委托调用,并阐述.NET如何将委托用作实现事件的方式. 参考:C#高级编程 1.什么是委托(delegate)? ...
- 委托学习过程及委托、Lambda表达式和匿名方法的关系总结及事件总结
第一章,当开始学习委托的时候,我们会问什么是委托?为什么要学习委托? 一,什么是委托? 委托是一个类,它定义了方法的类型,使得可以将方法当作另一个方法的参数来进行传递,这种将方法动态地赋给参数的做法, ...
- C#从委托、lambda表达式到linq总结
前言 本文总结学习C#必须知道的基础知识,委托.监视者模式.常用lambda表达式.linq查询,自定义扩展方法,他们之间有什么关系呢?匿名委托是如何演变成lambda表达式,lambda再如何导出l ...
- 委托、匿名委托、Lambda 表达式、Expression表达式树之刨根问底
本篇不是对标题所述之概念的入门文章,重点在阐述它们的异同点和应用场景.各位看官,这里就不啰嗦了,直接上代码. 首先定义一个泛型委托类型,如下: public delegate T Function&l ...
- 使用匿名委托,Lambda简化多线程代码
使用匿名委托,Lambda简化多线程代码 .net中的线程也接触不少了.在多线程中最常见的应用莫过于有一个耗时的操作需要放到线程中去操作,而在这个线程中我们需要更新UI,这个时候就要创建一个委托了 ...
- 转载 C#匿名函数 委托和Lambda表达式
转载原出处: http://blog.csdn.net/honantic/article/details/46331875 匿名函数 匿名函数(Anonymous Function)是表示“内联”方法 ...
- 十二、C# 委托与Lambda表达式(匿名方法的另一种写法)
委托与Lambda表达式 1.委托概述 2.匿名方法 3.语句Lambda 4.表达式Lambda 5.表达式树 一.委托概述 相当于C++当中的方法指针,在C#中使用delegate 委托来 ...
- 委托与Lambda表达式
~,先不急说委托和Lambda表达式,先看两个例子再说: 1. 通过委托,为一个数字加10,如下代码: class Program { private delegate int JiSuan(int ...
- C#函数式程序设计之函数、委托和Lambda表达式
C#函数式程序设计之函数.委托和Lambda表达式 C#函数式程序设计之函数.委托和Lambda表达式 相信很多人都听说过函数式编程,提到函数式程序设计,脑海里涌现出来更多的是Lisp.Haske ...
随机推荐
- WPF 多语言实现
很多国际化的程序都提供了多语言的选项,这样方便不同国家的使用者更方便的使用软件.这篇博客中将介绍在WPF中实现多语言的方式. 方式一,使用WPF动态资源的方式实现.先简单介绍下StaticResour ...
- hdu 4751 2013南京赛区网络赛 二分图判断 **
和以前做过的一个二分图颇为相似,以前的是互相不认识的放在一组,这个是互相认识的,本质上是相同的 是 hdu 2444 #include<cstdio> #include<iostre ...
- 自己制作QQ空间音乐的具体方法
1.打开QQ邮箱找到左栏下方的“文件中转站”--点击收藏文件--上传到收藏 将MP3或WMA音乐文件上传 上传完成点下载 下图: 2.点“保存”将最上面一排的地址全部复制 下图 3.为了更 ...
- Linggle: 英语写作学习搜索引擎
Linggle 搜索引擎是一个可用于英语写作的语法.句子工具,可帮助学习者分析更准确的英文写作建议,能够根据词性来推测短句和句子,可精准的分享出完整英文句子如何撰写. Linggle 是台湾学术团队研 ...
- Unity3D使用过程中常见的20个问题
1:天空盒有接缝怎么解决?答:在贴图导入设置里设置Wrap Mode为"Clamp". 2:DDS格式怎么不显示?答:Unity不支持DDS格式,Unity会将除DDS外的其他格式 ...
- Oracle自动备份脚本(网上找到的资料)
废话不多说了,直接给大家贴代码了,具体代码如下所示: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 ...
- Java Math floor round ceil 函数
public final class Math extends Object public static double floor(double a) public static long round ...
- ASP.NET MVC5 网站开发实践(一) - 项目框架(转)
前几天算是开题了,关于怎么做自己想了很多,但毕竟没做过项目既不知道这些想法有无必要,也不知道能不能实现,不过邓爷爷说过“摸着石头过河”吧.这段时间看了一些博主的文章收获很大,特别是@kencery,依 ...
- js 事件监听
addEventListener() 方法 element.addEventListener(event, function, useCapture); 第一个参数是事件的类型 (如 "cl ...
- loopback 03
使用微信开发前准备 微信公众开发者平台 注册开发者账号获取权限: 安装包: wechat, wechat-oauth 微信公众平台操作 登录之后,得到appID和appsecret 根据appID和a ...