Ehsan Ghanbari

Experience, DotNet, Solutions

Exploring lambda expression in C#

Lambda expression is an anonymous function and inline delegate which lets you create a local function to pass arguments or return type as a value of the function. Although anonymous methods help you to create inline method anonymous methods force you to define the parameter and return types:


    delegate void NumberChanger(int n);
        NumberChanger _numberChanger = delegate(int x)


But a lambda expression is  a method without a declaration, access modifier, return value, and even any name:


   delegate void NumberChanger(int n);
   NumberChanger _numberChanger = x => Console.WriteLine("Anonymous Method: {0}", x);


Another example:


            int[] a = { 2, 3, 4, 5, 6, 7, 8, 9 };
            Func<int, bool> func = delegate(int l)       //func via delegate declaration
                 return l == 2;
            Func<int, bool> func = l => l == 2;          //func via lamda expression


the lambda expression is more fluent and easier and most convenient way to create a delegate. Note that Lambda expression has no meaning outside the context of the .NET. The lambda expression syntax uses the => operator: "Parameters => Executed code" for example : x => x * x  specifies a parameter with name x and returns the value of x squared. when you create a lambda, for example:


            int[] a = { 2, 3, 4, 5, 6, 7, 8, 9, 9 };
            int b = a.Count(n => n % 2 == 1);


It's actually a method with one parameter:


          int b = a.Count((n) => n % 2 == 1);


Parentheses are not required when the lambda has one input parameter, but if you have more than one input parameter you should put parentheses just like method definition:


            int d = a.First(i => ((i & 1) == 1));


Or if you have a delegate defined like this:


   delegate long X(int x, int y, int z);


 Your lambda  should be like this:


  X lambda= (x, y, z) => x * y * z


as you don't need to specify the name of the function and return type or an access modifier in lambda expression, so you don't need to look elsewhere for the method's definition when you reading your codes. But notice that Lambda expressions should be short because A complex definition makes the calling code difficult to read, maybe more difficult than writing anonymous methods! Cheers

About Me

Ehsan Ghanbari

Hi! my name is Ehsan. I'm a developer, passionate technologist, and fan of clean code. I'm interested in enterprise and large-scale applications architecture and design patterns and I'm spending a lot of my time on architecture subject. Since 2008, I've been as a developer for companies and organizations and I've been focusing on Microsoft ecosystem all the time. During the&nb Read More

Post Tags
Pending Blog Posts
Strategic design
Factory Pattern
time out pattern in ajax
Selectors in Jquery
Peridic pattern
How to use PagedList In MVC
Redis as a cache server
Domain driven design VS model driven architecture
How to query over Icollection<> of a type with linq
What's the DDD-lite?
comments powered by Disqus