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 ...
随机推荐
- poj 1195:Mobile phones(二维线段树,矩阵求和)
Mobile phones Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 14391 Accepted: 6685 De ...
- Codeforces Round #364 As Fast As Possible
二分思想,对所要花费的时间进行二分,再以模拟的形式进行验证是否可行. 使用二分法,可以将一个求最优解的问题转化为一个判定问题,优雅的暴力. #include<cstdio> #includ ...
- C# 创建Windows Service
当我们需要一个程序长期运行,但是不需要界面显示时可以考虑使用Windows Service来实现.这篇博客将简单介绍一下如何创建一个Windows Service,安装/卸载Windows Servi ...
- 在Activity和Application中使用SharedPreferences存储数据
1.在Activity中创建SharedPreferences对象及操作方法 SharedPreferences pre=getSharedPreferences("User", ...
- Ubuntu下Chromium for Android 源码的编译
转自:http://blog.csdn.net/leer168/article/details/9146689 一.环境Ubuntu10.4.4 -desktop-amd64 + VMware Wor ...
- WPF之MVVM(Step3)——使用Prism(1)
使用WPF-MVVM开发时,自己实现通知接口.DelegateCommand相对来说还是用的较少,我们更多的是使用第三方的MVVM框架,其中微软自身团队提供的就有Prism框架,此框架功能较多,本人现 ...
- ssh 免密码登陆
远程ssh登陆服务器或者其他机器时或者scp时,需要输入密码,感觉很麻烦,于是研究如何免密码登陆. step1:Client端生成公钥和密钥 执行命令 ssh-keygen 进入目录~/.ssh里面, ...
- Linux内核分析--操作系统是如何工作的
“平安的祝福 + 原创作品转载请注明出处 + <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 ” 一.初 ...
- mysql注入研究
网址: http://www.jb51.net/article/14446.htm http://www.jb51.net/article/29445.htm
- 新的开始—js客户端onlick无法响应
第一次写博客,也是为了以后遇到同样的问题时可以找到,onlick无法响应在百度一些都有解释,这只是一个个人记录而已 问题: function change_tp(ProInfo_id, color_i ...