I’m starting with Lambda Expressions to understand how to define anonymous methods using it.
I have the following simple code:
delegate void AddNumber(int number);
class LambdaExpressionSample
{
static void Main(string[] args)
{
AddNumber method = r => Console.WriteLine(r + r);Console.Read();
method(1);
}
}
What I would like is that the method would be equal to this:
AddNumber method = new AddNumber(delegate(int number)
{
Console.WriteLine(number+number);
Console.Read();
});
But the code with the lambda expression is not working properly because the lambda expression ends apparently with the semicolon “;” after the Console.WriteLine.
How could I make my method execute Console.WriteLine and Console.Read using my lambda example?
You have to use braces if you have more then one statement