1

I know that x => x + 1 mean create a anonymous function returning x + 1 but what does cfg => {} ; mean?

Jimmy Dfgh
  • 81
  • 5
  • 4
    http://stackoverflow.com/questions/1743013/is-there-a-way-to-specify-an-empty-c-sharp-lambda-expression – Zohar Peled May 09 '16 at 05:06
  • [Read here](https://msdn.microsoft.com/en-IN/library/bb397687.aspx) complete documentation about Lambda Expression. – Irfan May 09 '16 at 05:21

2 Answers2

2

It's a lambda that does nothing (i.e it's a void method that does no work).

This might be required for a method that requires an action delegate but you don't need the callback to do anything, so you just throw in an empty lambda to achieve the result.

Spence
  • 28,526
  • 15
  • 68
  • 103
  • and in that case, you wouldn't even need to name it, you could have written () => {}, which is an anonymous lambda that takes no parameters and returns no result. – Spence May 09 '16 at 23:23
1

It depends on the delegate type that expression is supposed to represent.

The general syntax of a lamda expression is in the form:

[params] => [expression]

or

[params] => { [method body] }

So you know what x => x + 1 means, but it's also equivalent to x => { return x + 1; }, provided it's assigned to a variable of type Func<TInput, TOutput>.

Given that, cfg => {} is a lamda that takes some input and returns nothing, provided it's assigned to avariable of type Action<TInput>. Remember, actions have no return type and the return statement is optional for such methods.

Jeff Mercado
  • 129,526
  • 32
  • 251
  • 272