locked
Question about Lambda Expressions

    Question

  • I am having some trouble understanding Lambda expressions seen in code. I think part of the trouble I'm having stems from not knowing what to call the '=>' operator when I'm reading through.

    Not sure how common this is for other programmers, but when I look at source code, I tend to 'translate' it in my head into a natural language. Like, when I see:

    foreach(int someInt in someInts){
        Console.WriteLine(someInt);
    }

    I tend to look at that and read it as: "For each integer called 'someInt' in the collection 'someInts', Write 'someInt' to the console."

    or when I see 'int a = b + c' I read it, naturally, as "Integer a equals b plus c".

    But I'm at a loss to come up with some natural language 'translation' for the => operator, which at present I've just been calling "the equals greater-than operator"...

    So, how do you 'read' that operator, when you scan through your source code in your head? If that makes any sense...

    Monday, March 06, 2006 3:17 AM

Answers

  • I usually read the => operator as "becomes" or "for which". For example,

    Func<int,int> f = x => x * 2;
    Func<Customer,bool> test = c => c.City == "London";

    reads as "x becomes x * 2" and "c for which c.City equals London".

    Anders

     

    Monday, March 06, 2006 9:55 PM

All replies

  • I usually read the => operator as "becomes" or "for which". For example,

    Func<int,int> f = x => x * 2;
    Func<Customer,bool> test = c => c.City == "London";

    reads as "x becomes x * 2" and "c for which c.City equals London".

    Anders

     

    Monday, March 06, 2006 9:55 PM
  • Ah, that makes sense. :)

    Thanks a lot Anders.

    Monday, March 06, 2006 10:00 PM