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 ...
随机推荐
- [LeetCode] Maximal Rectangle
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and ...
- ArcGIS ElementLayer上放置Windows控件
ElementLayer是ArcGIS API for Silverlight/WPF中的一种图层类型,主要用来承载Silverlight/WPF中的UIElement对象(UIElement),使用 ...
- play-framework的安装与使用
一.下载: 到http://www.playframework.com/download下载 解压好包,然后输入: activator ui 访问:http://127.0.0.1:8888/home
- RTCP资料详解
转自:http://www.360doc.com/content/13/0606/10/1317564_290865866.shtml RTCP RTCP协议将控制包周期发送给所有连接者,应用与数据包 ...
- Web开发基本准则-55实录-Web访问安全
Web开发工程师请阅读下面的前端开发准则,这是第一部分,强调了过去几年里我们注意到的Web工程师务须处理的Web访问安全基础点.尤其是一些从传统软件开发转入互联网开发的工程师,请仔细阅读,不要因为忽视 ...
- 趣味算法:字符串反转的N种方法(转)
老赵在反对北大青鸟的随笔中提到了数组反转.这的确是一道非常基础的算法题,然而也是一道很不平常的算法题(也许所有的算法深究下去都会很不平常).因为我写着写着,就写出来8种方法……现在我们以字符串的反转为 ...
- LayoutInflater(二)
每一个视图的绘制过程都必须经历三个最主要的阶段,即onMeasure().onLayout()和onDraw(),下面我们逐个对这三个阶段展开进行探讨. 一. onMeasure() measure是 ...
- 基于XMPP协议的Android即时通信系
以前做过一个基于XMPP协议的聊天社交软件,总结了一下.发出来. 设计基于开源的XMPP即时通信协议,采用C/S体系结构,通过GPRS无线网络用TCP协议连接到服务器,以架设开源的Openfn'e服务 ...
- 开始我的PostgreSQL的学习之旅
经过这么长时间的学习,终于确定了我的研究方向是PostgreSQL的空间数据库的设计流程,具体怎样实现这个过程,其难度是挺大的,我必须克服掉,尽量得往前看.大家有相同的研究方向的,可以一同来学习,相互 ...
- POJ 2750 Potted Flower (线段树区间合并)
开始懵逼找不到解法,看了网上大牛们的题解才发现是区间合并... 给你n个数形成一个数列环,然后每次进行一个点的修改,并输出这个数列的最大区间和(注意是环,并且区间最大只有n-1个数) 其实只需要维护 ...