CSharp lambda expressions in detail


C# lambda expressions in detail
lambda

image credit:http://www.takipiblog.com

With C# lambda expressions in detail, we will highlight the uses of these expressions in Visual Studio Development Environment

Why we use Lambda Expression?

It is not as difficult to understand the usage of Lambda Expressions in C# programming language.
All of us have seen that most websites make the concepts as difficult to explain as the theory of egg or chicken?

Lambda Expressions make the use of Anonymous Delegates quite easier to implement with proper programming patterns.
Lambda expressions arrived with the release of C# 3.0 and since then, they have become a daily practice for professional programmers.Let’s see what Microsoft is saying about the use of Lambda Expression.

“A lambda expression is an anonymous function that you can use to create delegates or expression tree types. By using lambda expressions, you can write local functions that can be passed as arguments or returned as the value of function calls. Lambda expressions are particularly helpful for writing LINQ query expressions”
Reference: MSDN

The symbol => is called as lambda operator.The input variables in lambda expression are in the left side of => .The body of lambda expression is on the right hand side of => operator.
Lambda Expressions are hugely used in LINQ(Language Integrated Query)

1.Lambda with Collections
Lets see them in examples:

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;

namespace myFirstExample

{

public class Program

{

private static void Main(string[] args)

{
int[] myArray = new[] { 1,2,3,4,5,6,7,8,9};
/* here b on the left side of => operator is an input variable
and b<=9 denotes that b is less than or equal to 9 and the loop iterates in myArray till the maximum value i.e 9 is achieved */ foreach (int a in myArray.Where(b => b <= 9)) Console.WriteLine(a); } } }

Output:
1
2
3
4
5
6
7
8
9

using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
namespace Csharp_programs
{
//delegate of name mydelegate return a as argument of type int
// the return type is int as well
delegate int mydelegate(int a);
class Program
{

static void Main(string[] args)
{
// x is on left hand side is an input argument in expression x=>x*x
// the right side has x*x means x times x assigned to input x on left side
// of =>
mydelegate expression = x => x * x;
// expression delegate returns the value to b
var b = expression(5);
Console.WriteLine(“b:” + b.ToString());
Console.ReadKey();
}
}
}

Output

b:25

using System;
using System.Collections.Generic;

class Program
{
static void Main()
{
//Generic List called numbers
List numbers = new List();
// adding values to the list
numbers.Add(1);
numbers.Add(2);
numbers.Add(3);
numbers.Add(4);

//an array object containing some numbers in its indices
int[] myArray = new int[4];
myArray[0] = 1;
myArray[1] = 2;
myArray[2] = 3;
myArray[3] = 4;

//adding the array elements to the numbers list
numbers.AddRange(myArray);

/*find those numbers that are greater than 3 with Find method and an expression
elements => elements > 3. Tratehe elements variable gets those elements that are greater than 3
int myresultSet = numbers.Find(elements => elements > 3);

Console.WriteLine(myresultSet);
Console.ReadKey();
}
}

Output

4

Also read CSharp Lambda in Detail2

Syed Adeel Ahmed
Analyst, Programmer, Educationist and Blogger at Technofranchise
Computer Systems Engineer from Sir Syed University Of Engineering & Technology.I am passionate about all types of programming.
Syed Adeel Ahmed on FacebookSyed Adeel Ahmed on GoogleSyed Adeel Ahmed on InstagramSyed Adeel Ahmed on Twitter

Published by

Syed Adeel Ahmed

Computer Systems Engineer from Sir Syed University Of Engineering & Technology.I am passionate about all types of programming.