Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 something: {0}", () => action.GenerateDescription()); 

Question is, what is this ()=> operator, I wonder? I tried Googling it but since it's made of symbols Google couldn't be of much help, really. Did I embarrassingly miss something here?

like image 698
Orca Avatar asked Sep 02 '10 14:09

Orca


People also ask

What is -> in C language?

An Arrow operator in C/C++ allows to access elements in Structures and Unions. It is used with a pointer variable pointing to a structure or union.


2 Answers

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; } 
like image 192
Simon Steele Avatar answered Sep 20 '22 19:09

Simon Steele


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

syntax: (input parameters) => expression

like image 29
htr Avatar answered Sep 19 '22 19:09

htr