The delegate operator creates an anonymous method that can be converted to a delegate type:

C#CopyRun
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:

C#CopyRun
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:

C#CopyRun
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

int[] numbers = { 1, 4, 7, 10 }; int product = numbers.Aggregate(1, (interim, next) => interim * next);
 
public override string ToString() => $"{fname} {lname}".Trim();
public override string ToString() { return $"{fname} {lname}".Trim(); }
 
no need of type and return!!!!!

delegate operator (C# reference) and => operator (C# reference)的更多相关文章

  1. operator new与new operator的区别

    原文地址:http://www.cnblogs.com/jamesmile/archive/2010/04/17/1714311.html,在此感谢 C++中的operator new与new ope ...

  2. operator new,new operator,placement new的区别

    原文地址:http://www.cnblogs.com/jamesmile/archive/2010/04/17/1714311.html,在此感谢 C++中的operator new与new ope ...

  3. undefined reference to 'dlopen';undefined reference to 'dlclose';undefined reference to 'dlerror'等问题

    在linux下,编译链接的时候,经常会遇到这样一个问题,undefined reference to.....,引起这个问题的原因在于在链接的时候缺少选项.下面举几个例子,并给出解决办法. 1.  u ...

  4. variadic templates & pass by const reference & member operator [] in const map & gcc sucks

    /// bugs code with comments #include <iostream> #include <memory> #include <unordered ...

  5. C++ 类型转换操作与操作符重载 operator type() 与 type operator()

    类型转换操作符(type conversion operator)是一种特殊的类成员函数,它定义将类类型值转变为其他类型值的转换.转换操作符在类定义体内声明,在保留字 operator 之后跟着转换的 ...

  6. C++ operator new和new operator的区别

    new operator 当你写这种代码: string *ps = new string("Memory Management"); 你使用的new是new  operator. ...

  7. jetSonNano darknet ubdefined reference to 'pow',undefined reference to 'sqrtf'....

    我在用CMakelist编译工程时,遇到了这个一连串基础数学函数找不到的问题,如下图所示: 我当时在工程中明明引用了 #include "math.h"头函数,这是因为你的工程在预 ...

  8. 5.Primitive, Reference, and Value Types

    1.Programming Language Primitive Types primitive types:Any data types the compiler directly supports ...

  9. operator new3种情况详解

    [本文链接] http://www.cnblogs.com/hellogiser/p/operator-new.html [代码]  C++ Code  12345678910111213141516 ...

随机推荐

  1. 008-elasticsearch5.4.3【二】ES使用、ES客户端、索引操作【增加、删除】、文档操作【crud】

    一.ES使用,以及客户端 1.pom引用 <dependency> <groupId>org.elasticsearch.client</groupId> < ...

  2. 阶段1 语言基础+高级_1-3-Java语言高级_06-File类与IO流_07 缓冲流_7_练习_对文本的内容进行排序

    出师表,按照12345678进行排序 使用Map集合进行排序 把内容都写到一行里面去了

  3. vue集成汉字转拼音或提取首字母

    需求:             有时我们为了节省用户的维护量,需要根据中文生成出相应的拼音和缩写 解决:            此方法是利用汉字和Unicode编码对应找到相应字母 一.编写汉字和编码 ...

  4. 如何实现动态水球图 --》 echars结合echarts-liquidfill实现

    1)项目中作为项目依赖,安装到项目当中(注意必须要结合echars) npm install echarts vue-echarts --save npm install echarts-liquid ...

  5. 谷歌,火狐浏览器不能禁用自动补齐的bug缺陷

    IE浏览器里有autocomplete="off",可以禁止自动补全账号和密码,为了防止信息泄露,需要去除自动补齐. 自动补齐产生的场景是,form里面有密码框,因此只要将该密码框 ...

  6. Java 操作pdf与excel

    java 操作pdf组件  itextpdf <dependency> <groupId>com.itextpdf</groupId> <artifactId ...

  7. SpringBoot(八) -- SpringBoot与Docker

    一.Docker简介 Docker是一个开源的应用容器引擎,基于Go语言并遵从Apache2.0协议开源.Docker可以让开发者打包他们的应用以及依赖到一个轻量级,可移植的容器中,然后发布到任何流行 ...

  8. Mybatis-学习笔记(2)Mybatis配置文件

      3>typeAliases:类型别名.2种指定方式. 1>给某个类起个别名 <typeAliases> <typeAlias type="com.lfy.b ...

  9. Appscan工作原理详解

    AppScan,即 AppScan standard edition.其安装在 Windows 操作系统上,可以对网站等 Web 应用进行自动化的应用安全扫描和测试. Rational AppScan ...

  10. Ubuntu 安装 ansible

    sudo apt update sudo apt-get install software-properties-common sudo apt-add-repository --yes ppa:an ...