tags:

views:

176

answers:

4

Ok, very silly question.

x => x * 2

is a lambda representing the same thing as a delegate for

int Foo(x) { return x * 2; }

But what is the lambda equivalent of

int Bar() { return 2; }

??

Thanks a lot!

+7  A: 

That would be:

() => 2

Example usage:

var list = new List<int>(Enumerable.Range(0, 10));
Func<int> x = () => 2;
list.ForEach(i => Console.WriteLine(x() * i));
Ahmad Mageed
+6  A: 

You can just use () if you have no parameters.

() => 2;
Steven Robbins
+11  A: 

The nullary lambda equivalent would be () => 2.

outis
Damn, that was fast :) Thanks everyone!
Luk
+1  A: 

The lmabda is:

() => 2
Pawel Lesnikowski