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. 阶段1 语言基础+高级_1-3-Java语言高级_1-常用API_1_第3节 Random类_10-练习一_生成1-n之间

    生成随即数的时候,总是会短一个,因为它是从0开始的 这是左闭右闭区间. 循环100次输出的结果

  2. jmeter之自动重定向和跟随重定向用法

    jmeter工具里面有自动重定向和跟随重定向这2种选择,那么他们到底有啥区别呢? 目录 1.自动重定向和跟随重定向 2.举个例子 1.自动重定向和跟随重定向 01.3XX的请求一般要使用跟随重定向,2 ...

  3. LeetCode——141 设计链表

    题目: 简单说下思路: 用两个指针,一个跑得快,一个跑得慢(例如一个每次前进两步,一个前进一步),这样只要快指针不会撞上NULL(如果遇到了NULL的情况那么必然不存在环),快指针肯定会和慢指针碰面( ...

  4. DataTable clone()和copy()的区别

    clone()只是复制表结构 copy()是深度复制,表结构和数据

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

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

  6. [Web 前端] 008 css 颜色表示方法

    css 颜色表示法 颜色名表示 如 red 红色 green 绿色 blue 蓝色 16 进制数值表示 常见颜色 正常表示 缩写表示 红色 #ff0000 #f00 绿色 #00ff0 #0f0 蓝色 ...

  7. JAVA总结--代码规范

    一.命名规范 1.标识符:统一.达意.简洁 统一:一个词有多种表达方式,不求最好,但求统一:例:供应商,既可以用supplier,也可以用provider,选择一种统一使用: 达意:明确表达其意义,正 ...

  8. Vert.x学习第一天

    昨天看了下异步,然后就开始了Vert.x相关知识的学习. Vert.x是当下非常流行的一套全异步框架,其优势在于轻量级.高效.非常适合作为移动端后台或是企业应用. 当然对于第一天接触这个框架的人(没错 ...

  9. java 快速开发平台 有代码生成器 springmvc SSM后台框架源码

    .   权限管理:点开二级菜单进入三级菜单显示 角色(基础权限)和按钮权限      角色(基础权限): 分角色组和角色,独立分配菜单权限和增删改查权限.      按钮权限: 给角色分配按钮权限.2 ...

  10. NancyFx 2.0的开源框架的使用-ConstraintRouting

    新建一个空的Web项目 然后在Nuget库中安装下面两个包 Nancy Nancy.Hosting.Aspnet 然后在根目录添加三个文件夹,分别是models,Module,Views 然后往Mod ...