Meaning of () => Operator in C#, if it exists

前端 未结 5 1072
余生分开走
余生分开走 2020-12-07 22:30

I read this interesting line here, in an answer by Jon Skeet.

The interesting line is this, where he advocated using a delegate:

Log.Info(\"I did som         


        
相关标签:
5条回答
  • 2020-12-07 23:08

    Creating an anonymous delegate to specified method.

    Probably, in your case it will be a Func<string>

    0 讨论(0)
  • 2020-12-07 23:17

    This is an example of a lambda expression you can learn more here.

    0 讨论(0)
  • 2020-12-07 23:18

    This introduces a lambda function (anonymous delegate) with no parameters, it's equivalent to and basically short-hand for:

    delegate void () { return action.GenerateDescription(); }
    

    You can also add parameters, so:

    (a, b) => a + b
    

    This is roughly equivalent to:

    delegate int (int a, int b) { return a + b; }
    
    0 讨论(0)
  • 2020-12-07 23:19

    It's way to pass anonymous delegate without parameters as lambda expression.

    Similar to this from .NET 2.0

    Log.Info("I did something: {0}", delegate()
                {
                    return action.GenerateDescription();
                });
    
    0 讨论(0)
  • 2020-12-07 23:22

    => this is lambda operator. When we don't have any input parameters we just use round brackets () before lambda operator.

    syntax: (input parameters) => expression

    0 讨论(0)
提交回复
热议问题