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 ...
随机推荐
- linux压缩文件命令-zip
首先cd到要压缩文件的目录,然后使用zip命令压缩文件 zip -r importExcel.zip importExcel -r表示递归 zip [参数] [打包后的文件名] [打包的目录路径] ...
- VUE(vue对象的简单属性)
一:全局过滤器和局部过滤器 ps:不管是局部过滤器还是全局过滤器,一定都要有renturn 返回 <!DOCTYPE html> <html lang="en"& ...
- 阶段1 语言基础+高级_1-3-Java语言高级_06-File类与IO流_09 序列化流_1_序列化和反序列化的概述
- 练习3-python-创造百万条数据库数据
有时候需求大批量的数据做测试支撑,如果使用传统的添加数据的方式可能比较耗费时间,利用python可以轻松的完成这项任务,还可以后续维护使用脚本. 方法1:insert into table selec ...
- 【ABAP系列】SAP ABAP 模拟做成像windows一样的计算器
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[MM系列]SAP ABAP 模拟做成像wind ...
- if you don't go after what you want, you'll never have it
conquest.n. 征服 quantitative: adj. 数量的 cellar: n. 地窖 roast. v. 烧烤 allowance. n. 津贴 drainage. n. 排水 ma ...
- K近邻实战手写数字识别
1.导包 import numpy as np import operator from os import listdir from sklearn.neighbors import KNeighb ...
- vue打包详情
说明 本文代码中的配置基于vue-cli2 需求 在实际开发中我们可能有测试环境一套请求API 和 正式环境一套API,尤其是两个环境的域名不同时,就需要我们分环境打不同配置的包 了解 webpack ...
- org.apache.ibatis.exceptions.PersistenceException: ### Error querying database. Cause: java.lang.NumberFormatException: For input string: "W%" ### Cause: java.lang.NumberFormatException: For input s
一个常见的myBatis xml文件中的引号错误: org.apache.ibatis.exceptions.PersistenceException: ### Error querying data ...
- String.indexOf()的使用方法
String.indexOf()的用途: 返回此字符串中第一个出现的指定的子字符串,如果没有找到则返回-1 源码如下: /** * Returns the index within this stri ...