You must Sign In to post a response.
  • Category: ASP.NET

    In which situation we can use Lambda expression?


    Are you looking for information on scenarios where Lambda expression can be used? Want to know how to use Lambda expression? Read this thread to learn more about Lambda expression with examples.



    Hi,
    In which situation we can use Lambda expression?
  • #722039
    Lambda expressions use as a special syntax. It allows to used data as variables or fields. The syntax use of lambda expression the( => )operator.
    Example code of Lambda expressions
    public static void LambdaExpression()
    {
    Func<int, int> squareA = x => x * x;
    Func<int, int> squareB = (x) => { return x * x; };

    Expression<Func<int, int>> squareexpression = x => x * x;

    Expression<Func<int, int>> squareB = (int x) => { return x * x; };.
    }

  • #722040
    Hi,

    Lambda Expression is anonymous type. It is formed with help of => operator. If you want to do any calculations in a single line like an method then you use lambda expression.

    Refer below simple example. I use lambda expression to find exact number present based on index


    int[] numbers = { 1, 3, 2, 8, 4 };
    var numsInPlace = numbers.Select((num, index) => new { Num = num, InPlace = (num == index) });
    foreach (var n in numsInPlace)
    {
    Console.WriteLine("{0}: {1}", n.Num, n.InPlace);
    }


    The above returns output mean exact value present in the index or not
    1: False
    3: False
    2: True
    8: False
    4: True

    Regards
    N.Ravindran
    Your Hard work never fails

  • #722058
    Hai Bibillis,
    Lambda Expression is the alternate way of Anonymous Methods and anonymous methods are used for the calling of more than one functions in a single statement without using the function name.
    Lets say, I want to insert the record in to listbox by button click and the record is in Textbox.
    We can write the anonymous method like:

    btn1.Click +=delegate{lstBox1.Items.Add(textBox1.text)};

    Hope it will be helpful to you.

    Regards,
    Pawan Awasthi(DNS MVM)
    +91 8123489140 (whatsApp), +60 14365 1476(Malaysia)
    pawansoftit@gmail.com

  • #734097
    A lambda express is an anonymous function that you can use to ceate express tree or delegates types.You can write local functions that can be passed as arguments or returned as the value of function calls by using lambda expressions. more reference about this kindly check this link http://msdn.microsoft.com/en-us/library/vstudio/bb397687.aspx



    Thanks & Regards
    G.Renganathan
    Nothing is mine ,Everything is yours!!!


  • This thread is locked for new responses. Please post your comments and questions as a separate thread.
    If required, refer to the URL of this page in your new post.