What is Lamda Operator? Explain with code.

 Posted by Nagasundar_Tn on 12/1/2012 | Category: C# Interview questions | Views: 3350 | Points: 40
Answer:

Lamda expression are concise way to represent an anonymous method that we can use to create delegates or expression tree types. Lambda expression is an inline delegate introduced with C # 3.0 language.

 static void Main(string[] args)

{
#region Using Lamda
int[] Marks = { 100,54,35,98,62,85,49 };


var FailMarks = Marks.Where(Fail => Fail < 50);

foreach (int failMarks in FailMarks)
Console.WriteLine(string.Format("Fail Marks are using LAMDA {0}", failMarks));
#endregion


#region Using Delegate
var delegateFail = Marks.Where(delegate(int x)
{
return (x < 50);
})
.ToList();


foreach (int failMarks in delegateFail)
Console.WriteLine(string.Format("Fail Marks are using DELEGATES {0}", failMarks));
#endregion
}


In the above code I am having integer array of marks ranging from 1-100. I need to print the marks which are lesser then 50 that is Fail. In first part I achieved using Lamda operator by a condition less then 50. In second part I created an anonymous delegate and checking the conditon.

The output will be
Fail Marks are using LAMDA 35

Fail Marks are using LAMDA 49
Fail Marks are using DELEGATES 35
Fail Marks are using DELEGATES 49


Asked In: Nest Technology | Alert Moderator 

Comments or Responses

Login to post response