delegate operator (C# reference) and => operator (C# reference)
The delegate operator creates an anonymous method that can be converted to a delegate type:
Func<int, int, int> sum = delegate (int a, int b) { return a + b; };
Console.WriteLine(sum(3, 4)); // output: 7
Note
Beginning with C# 3, lambda expressions provide a more concise and expressive way to create an anonymous function. Use the => operator to construct a lambda expression:
Func<int, int, int> sum = (a, b) => a + b;
Console.WriteLine(sum(3, 4)); // output: 7
For more information about features of lambda expressions, for example, capturing outer variables, see Lambda expressions.
When you use the delegate operator, you might omit the parameter list. If you do that, the created anonymous method can be converted to a delegate type with any list of parameters, as the following example shows:
Action greet = delegate { Console.WriteLine("Hello!"); };
greet();
Action<int, double> introduce = delegate { Console.WriteLine("This is world!"); };
introduce(42, 2.7);
// Output:
// Hello!
// This is world!
That's the only functionality of anonymous methods that is not supported by lambda expressions. In all other cases, a lambda expression is a preferred way to write inline code.
You also use the delegate keyword to declare a delegate type.
C# language specification
For more information, see the Anonymous function expressions section of the C# language specification.
See also
Feedback
Lambda operator
In lambda expressions, the lambda operator => separates the input parameters on the left side from the lambda body on the right side.
Send feedback about
delegate operator (C# reference) and => operator (C# reference)的更多相关文章
- operator new与new operator的区别
原文地址:http://www.cnblogs.com/jamesmile/archive/2010/04/17/1714311.html,在此感谢 C++中的operator new与new ope ...
- operator new,new operator,placement new的区别
原文地址:http://www.cnblogs.com/jamesmile/archive/2010/04/17/1714311.html,在此感谢 C++中的operator new与new ope ...
- undefined reference to 'dlopen';undefined reference to 'dlclose';undefined reference to 'dlerror'等问题
在linux下,编译链接的时候,经常会遇到这样一个问题,undefined reference to.....,引起这个问题的原因在于在链接的时候缺少选项.下面举几个例子,并给出解决办法. 1. u ...
- variadic templates & pass by const reference & member operator [] in const map & gcc sucks
/// bugs code with comments #include <iostream> #include <memory> #include <unordered ...
- C++ 类型转换操作与操作符重载 operator type() 与 type operator()
类型转换操作符(type conversion operator)是一种特殊的类成员函数,它定义将类类型值转变为其他类型值的转换.转换操作符在类定义体内声明,在保留字 operator 之后跟着转换的 ...
- C++ operator new和new operator的区别
new operator 当你写这种代码: string *ps = new string("Memory Management"); 你使用的new是new operator. ...
- jetSonNano darknet ubdefined reference to 'pow',undefined reference to 'sqrtf'....
我在用CMakelist编译工程时,遇到了这个一连串基础数学函数找不到的问题,如下图所示: 我当时在工程中明明引用了 #include "math.h"头函数,这是因为你的工程在预 ...
- 5.Primitive, Reference, and Value Types
1.Programming Language Primitive Types primitive types:Any data types the compiler directly supports ...
- operator new3种情况详解
[本文链接] http://www.cnblogs.com/hellogiser/p/operator-new.html [代码] C++ Code 12345678910111213141516 ...
随机推荐
- KETTLE——(一)资源库
对KETTLE有了大概的了解,pdi-ce-6.0.1.0-386也下载完成了. 1.解压pdi-ce-6.0.1.0-386.zip文件,双击运行Spoon.bat(KETTLE是Java开发的,运 ...
- window.open弹窗阻止问题解决之道
https://segmentfault.com/a/1190000015381923https://segmentfault.com/a/1190000014988094https://www.cn ...
- sprint test 添加事务回滚机制
1.原因: 单元测试的时候频繁操作数据库需要修改很多数据,造成不必要的操作,添加事务之后就可以重复对一条数据进行操作,并且在返回结果后进行回滚. 2.解决: 原先继承的是 AbstractJUnit ...
- 【ABAP系列】SAP 读取生产订单 记入文档的货物移动明细
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP 读取生产订单 记入文档的 ...
- Python3的基本数据类型
2.1. Python3中六个标准的基本数据类型: Number(数字) String(字符串) Sets(集合) Tuple(元组) List(列表) Dictionary(字典) 2.2. Pyt ...
- How to increase timeout for your ASP.NET Application ?
How to increase timeout for your ASP.NET Application ? 原文链接:https://www.techcartnow.com/increase-tim ...
- VSL基础
VSL是Virtools提供的一个非常有创意的脚本语言,它以输写代码的方式(区别于Virtools现有的图形化编程界面)进行编程开发,提高了程序的可读性和工作效率. 下图的功能是:将一个3D物体拷贝出 ...
- [Markdown] 01 简单应用 第一弹
目录 0. "调用函数前必先声明" 0.1 Table of Content 0.2 分割线 0.3 引用 0.4 标记 0.5 关于 html 0.6 代码块 用法 1 用法 2 ...
- accept()出的socket不会使用新的端口号
1 标识一个socket的是四元组,不只是端口号 client ip : client port : server ip : server port 2 accept出的新的socket仍然使用和li ...
- CentOS7 修复boot目录
这里为了达到实验目的,首先删除boot目录下所有内容 重启后发现系统进不去了,这正是我们想要的 进入系统救援模式,以重新引导系统 进入救援模式后,输入以下命令进行修复boot目录 重启后,能正常引导系 ...