The term ‘Lambda expression’ has derived its name from ‘lambda’ calculus which in turn is a mathematical notation applied for defining functions. Lambda expressions as a LINQ equation’s executable part translate logic in a way at run time so it can pass on to the data source conveniently. However, lambda expressions are not just limited to find application in LINQ only.
These expressions are expressed by the following syntax:
(input parameters) => expression or statement block
Below is an example of lambda expression
y => y * y
The above expression specifies a parameter named y and that value of y is squared. However, it is not possible to execute a lambda expression in this form. Example of a lambda expression in C# is shown below.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace lambdaexample
{
class Program
{
delegate int del(int i);
static void Main(string[] args)
{
del myDelegate = y => y * y;
int j = myDelegate(5);
Console.WriteLine(j);
Console.ReadLine();
}
}
No comments:
Post a Comment