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 ...
随机推荐
- 【MongoDB】 windows下建立双机主从
[双机配置] 服务端: 两台 Dell R730 双路E5 服务器 使用一个内网环境,网段20, ping 测试互通 主服ip: 192.168.20.176: 27017 从服ip: 192.168 ...
- 【GTS】关于GtsTetheringTestCases模块的几个失败项
GTS---关于GtsTetheringTestCases模块的几个失败项 1.run gts -m GtsTetheringTestCases -t com.google.android.tethe ...
- 【HANA系列】SAP HANA快捷键大全
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[HANA系列]SAP HANA快捷键大全 ...
- mysql使用触发器生成唯一订单号,
需求:订单号唯一,并且期望是时间格式加其他字符串, 实现:采用触发机制,在新增时根据新增id值加1作为订单生成的随机且确定唯一的数,因为id唯一: 遇到问题:新增时不能提前知道id值, 解决:取到当前 ...
- 前端 CSS的选择器 属性选择器
属性选择器,字面意思就是根据标签中的属性,选中当前的标签. 属性选择器 通常在表单控件中 使用比较多 根据属性查找 /*用于选取带有指定属性的元素.*/ <!DOCTYPE html> & ...
- 解决在data里面获取一个固定的img值
正常情况下在data里面申明,在img标签里面通过 :src应用就行了,但是如果是直接申明引用是没效果的: html: <div class="logo"> <i ...
- jQuery 的attr()与css()的区别
attr是attribute的缩写,意思是标签属性. css是,样式,意思是元素的style样式的 我的理解是: attr是操作元素的 属性 css是操作元素的 style属性 前者可以修改&l ...
- SCUT - 365 - 鹏哥的数字集合 - wqs二分 - 斜率优化dp
https://scut.online/p/365 https://www.luogu.org/problemnew/solution/P2365 写这篇的时候还不是很明白,看一下这个东西. http ...
- 问题 D: 小k的硬币问题
问题 D: 小k的硬币问题 时间限制: 1 Sec 内存限制: 128 MB提交: 21 解决: 5[提交] [状态] [命题人:jsu_admin] 题目描述 小k和小p一起玩一个游戏,有n堆硬 ...
- Oracle之共享服务器模式
在共享服务器体系结构中,一个dispatcher分派器将传入网络的多个会话请求定向到一个共享服务器进程池,消除了为每个连接分配一个专用服务器进程的需要.作为一般的指导原则,仅当系统需要并发连接到数据库 ...