Skip to main content

Revisiting Few C# Concepts - Delegates, Anon Methods, Lambdas, Expression Trees

image Let us explore few concepts about Delegates, Anonymous methods, Lambdas and Expression Trees. This is an introductory post, and I’ll follow up with more posts. These code examples are pretty simple, you can fire up a C# Console application in Visual Studio and try these examples if you are interested.

Delegates

We know that a delegate is a type that can reference a method. For example, consider this example to define a delegate and create a delegate instance.

   //Example 1 - Creating a delegate instance using Named method

   public delegate int BinaryOperationDelegate(int x,int y);
   
    class Program
    {
        static void Main(string[] args)
        {
            //Create a delegate instance using named method
            var add = new BinaryOperationDelegate(AddOperation);
            int result = add(20, 30);
            Console.WriteLine(result);
        }

        static int AddOperation(int x,int y)
        {
            return x+y;
        }
    }

The above code will give you an output of 50, no doubt, right ? :)

Anonymous Methods

Now, you may know that with C# 2.0, we can use anonymous methods to create a delegate instance with out a named method. Let us re-write the above implementation leveraging an anon method. Instead of the method name, you’ve to use the ‘delegate’ keyword. Also, note that there is no need to specify a return type – because the return type will be inferred from the delegate signature.

   

   //Example 2 - Creating a delegate instance using Anon method

   public delegate int BinaryOperationDelegate(int x,int y);

    class Program
    {
        static void Main(string[] args)
        {
            //Create a delegate instance using anon method
            BinaryOperationDelegate add= 
                    delegate(int a, int b) { return a+b;} ; 
            int result = add(20, 30);
            Console.WriteLine(result);
        }

    }

Lambdas

And of course, you might also know that with C# 3.0, you can use Lambdas for producing anonymous methods. A lamda expression is exactly a function, that can contain statements and expressions. Here is the equivalent of above code, still shortened using a lambda

    
   //Example 3 - Creating a delegate instance using statement lambda

    public delegate int BinaryOperationDelegate(int x, int y);
    class Program
    {
        static void Main(string[] args)
        {
            //Create a delegate instance using a statement lambda
            BinaryOperationDelegate add =(a,b)=>{
                                                //this statement block can contain 
                                                //multiple statements  
                                                return a+b; 
                                                };

            int result = add(20, 30);
            Console.WriteLine(result);
        }


    }    

In the above example, note the lambda syntax that replaces the anon method syntax in example 2
(a,b)=>{
            return a+b; 
       };

The types of input parameters a and b and the return type will be inferred correctly by the compiler, based on the context. How ever, on scenarios where the compiler can't infer the type directly, you can provide that explicitly. Like this.

    BinaryOperationDelegate add =(int a, int b)=>{ return a+b; };

There are basically two type of lambdas - Statement Lamdas and Expression Lambdas. The above example shows a statement lambda, as the right hand expression is inside a statement block, of the form
(param1, param2)=> { statement1; statement2; } ;

If you won't have input parameters, you may use the syntax
()=> { statement1; statement2; } ;

=> simply means 'goes to', and you may say that the input parameters goes to the statements (for some processing) :)

Statement Lambdas And Expression Lambdas


We’ve seen how to create Statement lambdas in the above example. Now, let us explore a bit about Expression Lambdas (or single line lambdas). The syntax is similar to statement lambdas, but for expression lambdas, the right hand expression should not be inside a statement block  { }. Instead of having statements on the right side, an Expression Lambda can have a single line expression on the right hand side. Here is the simple example, with out the statement block.
    public delegate int BinaryOperationDelegate(int x, int y);

    class Program
    {
        static void Main(string[] args)
        {
            //Expression Lambda
            BinaryOperationDelegate add =(a, b)=>a+b;
            int result = add(20, 30);

            Console.WriteLine(result);
        }
    }
The above code produces the same result as in the case of stament lambdas. How ever, Expression lamdba has an advantage over statement lambda. When an expression lambda is assigned to a variable of type Expression, the compiler emits code to build an expression tree that represents the lambda expression, instead of a delegate instance.
   //Example 4 - Expression Lambdas

    public delegate int BinaryOperationDelegate(int x, int y);

    class Program
    {
        static void Main(string[] args)
        {
            //Expression Lambda to delegate instance
            BinaryOperationDelegate add =(a, b)=>a+b;
            int result1 = add(20, 30);

            //Expression Lambda to Expression tree
            Expression<BinaryOperationDelegate> addExpr = (a, b) => a + b;

            //Let us compile the expression tree before executing the same
            var compiledAdd = addExpr.Compile();
            int result2=compiledAdd(20,30);
            

            Console.WriteLine(result1);
            Console.WriteLine(result2);
        }
    }


In the above example, you saw that we are assigning our expression lambda to the System.Linq.Expression<TDelegate> to create an expression tree, and the compiling it to get the actual delegate. Expression trees provide us a powerful way to represent code/logic as data, that can be modified and/or interpreted at runtime if required.

Example Usages for Lambdas

These are some of the areas you may find using Lambdas pretty often.

To provide inline event handlers like

 button.Click += (sender,args) =>
{
};

To find items in a collection

  var dogs= animals.Where(animal => animal.Type == "dog");

For iterating a collection, like

 animals.ForEach(animal=>Console.WriteLine(animal.Name));

Creating a custom object

 var myObj= mySource.Select(x => new {Name = x.name, Age= x.age});

Simple one line methods

 Func<int, int> add = x => x + x;

For aggregate operations

 double charCount = document.Sum(word => word.Length)

What is next?

As mentioned, this was an intro post for a couple of interesting posts I've in mind. I'll follow up this post with few more posts on Generic Delegates, Delegate Chaining, Type inference in generic delegates and methods and why they are interesting, Creating and parsing expression trees using Expression tree APIs etc. Stay tuned

Popular posts from this blog

Top 7 Coding Standards & Guideline Documents For C#/.NET Developers

Some time back, I collated a list of 7 Must Read, Free EBooks for .NET Developers, and a lot of people found it useful. So, I thought about putting together a list of Coding Standard guidelines/checklists for .NET /C# developers as well.As you may already know, it is easy to come up with a document - the key is in implementing these standards in your organization, through methods like internal trainings, Peer Reviews, Check in policies, Automated code review tools etc. You can have a look at FxCop and/or StyleCop for automating the review process to some extent, and can customize the rules based on your requirements.Anyway, here is a list of some good Coding Standard Documents. They are useful not just from a review perspective - going through these documents can definitely help you and me to iron out few hidden glitches we might have in the programming portion of our brain. So, here we go, the listing is not in any specific order.1 – IDesign C# Coding StandardsIDesign C# coding stand…

Creating a quick Todo listing app on Windows using IIS7, Node.js and Mongodb

As I mentioned in my last post, more and more organizations are leaning towards Web Oriented Architecture (WOA) which are highly scalable. If you were exploring cool, scalable options to build highly performing web applications, you know what Node.js is for.After following the recent post from Scott Hanselman, I was up and running quickly with Node.js. In this post, I’ll explain step by step how I’ve setup Node.js and Mongodb to create a simple Todo listing application.Setting up Node.jsThis is what I’ve done.1 – Goto http://nodejs.org/, scroll down and download node.exe for Windows, and place it in your c:\node folder2 – Goto IIS Node project in Git at https://github.com/tjanczuk/iisnode, download the correct ‘retail’ link of IIS Node zip file (I downloaded the already built retail package, otherwise you can download and build from the source).3 – Extract the zip file some where, and run the install.bat or install_iisexpress.bat depending on your IIS Version. If you don’t have IIS in…

5 Awesome Learning Resources For Programmers (To help you and your kids to grow the geek neurons)

Happy New Year, this is my first post in 2012. I’ll be sharing few awesome learning resources I’ve bookmarked, and will be pointing out some specific computer/programming related courses I've found interesting from these resources.Also, thought about saving this blog post for my kids as well - instead of investing in these Child education schemes (though they are too small as of today, 2 years and 60 days respectively ). Anyway, personally my new year resolution is to see as much videos from this course collections (assuming I can find some free time in between my regular job && changing my babies diapers).1 – Khan AcademyAs I mentioned some time back, you and your kids are missing some thing huge if you havn’t heard about Khan Academy.  It is an awesome learning resource, especially if you want to re-visit your basics in Math, Science etc.With a library of over 2,600 videos covering everything from arithmetic to physics, finance, and history and 268 practice exercises, th…