lambda is an anonymous delegate instance.
Suppose we have a delegate:
delegate int Calculate(int i);
We can create an lambda expression as follows:
Calculate c = x => x+1;
x is the parameter and x+1 is the method body.
Put everything together.
using System;
delegate int Calculate(int i);
class Program
{
public static void Main()
{
Calculate c = x => x + 1;
Console.WriteLine(c(1));
}
}
The output:
2
Lambda has the following syntax.
(parameters) => expression-or-statement-block
The parameters are defined by the delegate and the return type of expression-or-statement-block must be consistant with delegate return type.
We can use return type in lambda expression.
using System;
delegate int Calculate(int i);
class Program
{
public static void Main()
{
Calculate c = x => {return x + 1;}
Console.WriteLine(c(1));
}
}
The result:
2
java2s.com | Contact Us | Privacy Policy |
Copyright 2009 - 12 Demo Source and Support. All rights reserved. |
All other trademarks are property of their respective owners. |