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 ...
随机推荐
- Eclipse设置控制台日志输出位置
1.选择服务器配置 2.设置输出文件路径
- Delphi XE2 之 FireMonkey 入门(8) - TImage
TImage 主要成员: { 属性 } Bitmap : TBitmap; //图像 BitmapMargins : TBounds; ...
- 搭建 Git 服务器(基于 CentOS 7)
服务器上的-Git-架设服务器-官网参考 对于规模比较小的团队,可以直接搭建 Git 服务器,逐个收集研发同学的证书配置进来即可.如果团队规模比较大,可以直接采用 GitLab.Drone 等现成的带 ...
- Haddop的数据计算部分原理
import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FSDataInputStream; import o ...
- unity editor 折叠树
https://blog.csdn.net/e295166319/article/details/52370575 需要两个类:树节点类和界面实现类 1:树节点类(TreeNode) using Un ...
- clinical-逻辑核查数据的操作
1. 前端页面样式 2. 前端代码 添加: 展示: 修改 删除 3. 后台代码 封装的DAO类数据 # coding: utf-8 from pdform.services.db.dbCore imp ...
- 关于微信授权和unionid 的获取思路。
1.首先根据appid 获取到预授权码的code string Appid = "******";//appid.由于网页授权与js-jdk使用不同微信,所以暂时独立于此处. st ...
- 前端项目中使用jsencrypt进行字段加密
前端项目中使用jsencrypt进行字段加密. 使用步骤:①获取公钥②实例化对象③设置公钥④将所需数据进行加密然后返回. 进行一个简单的封装如下 /** * npm install jsencrypt ...
- linux--常用工具软件
三大远程连接工具 crt notepad++ filezilla
- oracle--角色权限
将不同权限赋予角色,再将角色赋予用户,起到管理权限的作用 SQL> create role myrole; 角色已创建. SQL> grant create session to myro ...