locked
lambda? RRS feed

  • Question

  • What is the difference?

    bool b = MyTest(x * x)

    and

    bool b = MyTest(x => x*x)

    Thanks


    Jon Jacobs<br/> Not sent from my iPhone, because I don't have one.

    Sunday, April 17, 2016 2:03 AM

Answers

  • Hi,

    in bool b = MyTest(x*x), MyTest has to take the result of x*x. So if x is an Integer, then MyTest must be a method like
    bool MyTest(int arg)

    So x*x is calculated in your code and then the result is given to MyTest.

    In bool b = MyTest(x => x*x), MyTest has to take a delegate.
    x => x*x is an anonymous function. MyTest is not getting an integer as in the first example. Instead it gets a delegate to the anonymous function. 

    You can have a look at https://msdn.microsoft.com/en-us/library/bb397687.aspx - you will find an example in there:

    delegate int del(int i);
    static void Main(string[] args)
    {
        del myDelegate = x => x * x;
        int j = myDelegate(5); //j = 25
    }

    Here you see: x => x*x is a delegate and your complete code could be something like:

    delegate int del(int i);
    
    static bool MyTest(int x)
    {
      return x > 100;
    }
    
    static bool MyTest(del myDelegate)
    {
      return myDelegate(11) > 100;
    }
    
    static void Main(string[] args)
    {
      int x = 5;
      bool b1 = MyTest(x*x); // Will calculate 5*5 and then call MyTest1 with the result.
      bool b2 = MyTest(x => x*x); // will create a function that takes an argument and then returns argument * argument. 
    }

    So you see: The local variable x is not used in the second call. Instead inside that method, the delegate is called with 11. So for x=>x*x it will return true but if you give the delegate x=>x+x it will return false.

    With kind regards,

    Konrad

    • Marked as answer by Kristin Xie Monday, April 25, 2016 7:35 AM
    Sunday, April 17, 2016 5:27 AM
  • Can you show the definition of MyTest in your example?

    You do not need delegates to use lambdas. But as I wrote: You have anonymous methods that way so you need something that handles the method. One way is delegates as  I had inside my example.

    But you see the difference? Your first example is doing this:
    var temp = x*x;
    bool b1 = MyTest(temp);
    So the x*x is nothing that the method sees or knows. It is just your main code.

    In bool b2 = MyTest( x=> x*x); 
    You create a function that takes an Argument x and then returns x*x. Your local variable x is not used at all. You do not need a local variable x.
    And the Method MyTest is not getting a result. It gets a function tht it can call. So it needs the data already.

    That is how all the stuff works (and it is quite powerfull!) So you can have any data e.g. an array and the work on the data. Select something, change something, filter something. It is not just direct database access as it was shown by DA924x. You could also use it on a lot of data structures to select specific items or sort or whatever you want to do. So it is really powerfull and you do not have to write code that iterarates through you data and processes it - instead you write some quite simple sinple linq expressions. Ok, "quite simple" is relative. It can be really complex. In fact I have to confess, that I write my code the "old"way. And I use ReSharper which often jumps in and tells me, that it could be written as linq statement. I let ReSharper do the conversion to see if it can be read easily. If the linq expression can be read easily, then I keep it. But if there are any doubts then I will go back to my previous code.

    But that lead us to some other points which are not really on topic now.

    With kind regards,

    Konrad

    • Marked as answer by Kristin Xie Monday, April 25, 2016 7:35 AM
    Monday, April 18, 2016 8:13 AM

All replies

  • Hi,

    in bool b = MyTest(x*x), MyTest has to take the result of x*x. So if x is an Integer, then MyTest must be a method like
    bool MyTest(int arg)

    So x*x is calculated in your code and then the result is given to MyTest.

    In bool b = MyTest(x => x*x), MyTest has to take a delegate.
    x => x*x is an anonymous function. MyTest is not getting an integer as in the first example. Instead it gets a delegate to the anonymous function. 

    You can have a look at https://msdn.microsoft.com/en-us/library/bb397687.aspx - you will find an example in there:

    delegate int del(int i);
    static void Main(string[] args)
    {
        del myDelegate = x => x * x;
        int j = myDelegate(5); //j = 25
    }

    Here you see: x => x*x is a delegate and your complete code could be something like:

    delegate int del(int i);
    
    static bool MyTest(int x)
    {
      return x > 100;
    }
    
    static bool MyTest(del myDelegate)
    {
      return myDelegate(11) > 100;
    }
    
    static void Main(string[] args)
    {
      int x = 5;
      bool b1 = MyTest(x*x); // Will calculate 5*5 and then call MyTest1 with the result.
      bool b2 = MyTest(x => x*x); // will create a function that takes an argument and then returns argument * argument. 
    }

    So you see: The local variable x is not used in the second call. Instead inside that method, the delegate is called with 11. So for x=>x*x it will return true but if you give the delegate x=>x+x it will return false.

    With kind regards,

    Konrad

    • Marked as answer by Kristin Xie Monday, April 25, 2016 7:35 AM
    Sunday, April 17, 2016 5:27 AM
  • What do you mean what is the difference?

    One is using lambda => operator and the other is not

    https://msdn.microsoft.com/en-us/library/bb311046.aspx

    As the link explains, lambda expression or most effective when using Linq.

    var atleastoneauthor = (from a in mView.DtoAuthors.Where(a => a.IsUpdate || a.AuthorID == -1)
     Select a).FirstOrDefault();
    

    var authors = GetAuthors()Where(x => x.IsUpdate);   // Linq and using lambda is filtering the collection of DtoAuthors being returned out of the collection.

    Sunday, April 17, 2016 5:32 AM
  • GetAuthors().Where(x => x.IsUpdate);  
    Sunday, April 17, 2016 5:36 AM
  • Thank you, Konrad. I get it, sort of. I've seen the delegate usage in some online tutorials. My problem is I see many, many statements like bool b = MyTest(x => x*x); in the code in a book I'm reading, but there are no delegates declared in the code, so I'm totally confused.

    Jon Jacobs<br/> Not sent from my iPhone, because I don't have one.

    Sunday, April 17, 2016 10:01 PM
  • Thank you, DA924x. >One is using lambda => operator and the other is not

    That much I know. What I wondered is if they would produce different results and why. I am reading a book with very many lambda expressions in the code, none of which involves LINQ, and no delegates are declared, so I am quite mystified.


    Jon Jacobs<br/> Not sent from my iPhone, because I don't have one.

    Sunday, April 17, 2016 10:06 PM
  • Thank you, DA924x. >One is using lambda => operator and the other is not

    That much I know. What I wondered is if they would produce different results and why. I am reading a book with very many lambda expressions in the code, none of which involves LINQ, and no delegates are declared, so I am quite mystified.


    Jon Jacobs<br/> Not sent from my iPhone, because I don't have one.


    The only time I use Lambda other than with using Linq is when Resharper suggests a coding change and converts it to a Lambda expression.  :)
    Monday, April 18, 2016 3:25 AM
  • Hehe ... more or less the same here :)
    Monday, April 18, 2016 7:52 AM
  • Can you show the definition of MyTest in your example?

    You do not need delegates to use lambdas. But as I wrote: You have anonymous methods that way so you need something that handles the method. One way is delegates as  I had inside my example.

    But you see the difference? Your first example is doing this:
    var temp = x*x;
    bool b1 = MyTest(temp);
    So the x*x is nothing that the method sees or knows. It is just your main code.

    In bool b2 = MyTest( x=> x*x); 
    You create a function that takes an Argument x and then returns x*x. Your local variable x is not used at all. You do not need a local variable x.
    And the Method MyTest is not getting a result. It gets a function tht it can call. So it needs the data already.

    That is how all the stuff works (and it is quite powerfull!) So you can have any data e.g. an array and the work on the data. Select something, change something, filter something. It is not just direct database access as it was shown by DA924x. You could also use it on a lot of data structures to select specific items or sort or whatever you want to do. So it is really powerfull and you do not have to write code that iterarates through you data and processes it - instead you write some quite simple sinple linq expressions. Ok, "quite simple" is relative. It can be really complex. In fact I have to confess, that I write my code the "old"way. And I use ReSharper which often jumps in and tells me, that it could be written as linq statement. I let ReSharper do the conversion to see if it can be read easily. If the linq expression can be read easily, then I keep it. But if there are any doubts then I will go back to my previous code.

    But that lead us to some other points which are not really on topic now.

    With kind regards,

    Konrad

    • Marked as answer by Kristin Xie Monday, April 25, 2016 7:35 AM
    Monday, April 18, 2016 8:13 AM