zoukankan      html  css  js  c++  java
  • delegate operator (C# reference) and => operator (C# reference)

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

    C#
    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#
    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#
    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!!!!!
  • 相关阅读:
    Linux下中文乱码
    hive配置元数据库mysql文件配置
    centos7安装mysql5.6
    mapreduce案例:获取PI的值
    hadoop第一个程序WordCount
    centos7搭建伪分布式集群
    Centos7永久关闭防火墙
    centos7设置静态ip
    大数据技术之kettle
    2020/6/20 mysql表连接和子查询
  • 原文地址:https://www.cnblogs.com/gaoxianzhi/p/11946779.html
Copyright © 2011-2022 走看看