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. floding regions

  2. Java ——数组 选择排序 冒泡排序

    本节重点思维导图 数组 public static void main(String[] args) { int a ; a=3; int[] b; b = new int[3];//强制开辟内存空间 ...

  3. JS使用 popstate 事件监听物理返回键

    pushHistory();        window.addEventListener("popstate", function (e) {            if (or ...

  4. CentOS 7 关闭启动防火墙

    关闭firewall:systemctl stop firewalld.service #停止firewallsystemctl disable firewalld.service #禁止firewa ...

  5. 加密算法:DES、AES等

    指标:运算速度.安全性.资源消耗 对称加密算法(加解密密钥相同): 非对称算法(加密密钥和解密密钥不同): 散列算法比较: 对称与非对称算法比较: 算法选择(从性能和安全性综合) 对称加密: AES( ...

  6. HTMLTestRunner_PY3脚本代码

    HTMLTestRunner_PY3.py文件代码如下: # -*- coding: utf-8 -*- """ A TestRunner for use with th ...

  7. SCAU 2015 GDCPC team_training0

    A.题目:http://acm.timus.ru/problem.aspx?space=1&num=2024 题意:求一个包含K个不同字符的集合的最大长度,还有组成这个长度的集合的个数 做法: ...

  8. centos安装httprunner方法

    测试脚本执行的环境部署(在jenkins服务器中,要求jenkins服务器和目标的灰度环境是连通的): 一.安装python3.8 $python#看见2.6.6Python 2.6.6 (r266: ...

  9. RequireJS 入门(二)

    简介 如今最常用的JavaScript库之一是RequireJS.最近我参与的每个项目,都用到了RequireJS,或者是我向它们推荐了增加RequireJS.在这篇文章中,我将描述RequireJS ...

  10. OA是Office Automation

    OA是Office Automation OA是Office Automation OA是Office Automation