Thursday, June 14, 2012

The Evolution of Delegates from .Net 1.1 to .Net 3.5


In .Net 1.1, delegates were initialized by mapping them to explicit method names which are already defined. To more about delegates refer to the post Delegates in C#.

In .Net 2.0, Anonymous methods helped us to define the methods while initializing the delegates. To more about Anonymous Methods refer to the post Anonymous Methods.

In .Net 3.0 Lambda expressions further simplify delegate programming by adding expression based syntax. To more about Lambda Expressions refer to the post Lamba Expressions.

Let us understand Delegate programming in various versions of .net with an example.  

First let us declare a Delegate, this syntax remains the same in all the versions.

delegate
int AddNumberDelegate(int Number1, int Number2);

Now let us initialize the delegate, and invoke the delegate

Delegates in .Net 1.1

In .Net 1.1 to initialize this delegate, we need to declare a function which will match the signature of the delegate, and later assign the function while initializing the delegate as follows.

To more about delegates refer to the post Delegates in C#.

First let us declare a function with the same signature of the Delegate.

private int AddNumber(int Number1, int Number2)
{
    return Number1 + Number2;
}

Now we shall initialize the delegate by mapping it to the above function.

// Delegates in .Net 1.1
// Delegate Initialization
AddNumberDelegate Delegate_v1 = new AddNumberDelegate(AddNumber);
// Delegate Call
Response.Write(".Net 1.1 Delegate: " + Delegate_v1(1, 2));

Delegates in .Net 2.0 (Anonymous Methods)
In .Net 2.0, we can define the delegate body along with the delegate initialization, using Anonymous methods. As you could see, there is no need to define an explicit method; the function body is in line with the delegate initialization.

To more about Anonymous Methods refer to the post Anonymous Methods.


// Delegates in .Net 2.0
AddNumberDelegate Delegate_v2 = delegate(int Number1, int Number2)
                                {
                                    return Number1 + Number2;
                                };
// Delegate Call
Response.Write(".Net 2.0 Delegate: " + Delegate_v2(1, 2));

Delegates in .Net 3.0 (Lambda Expressions)
.Net 3.0, further simplifies delegate initialization, it uses symbolic representation using the Lambda Expression =>.  To more about Lambda Expressions refer to the post Lamba Expressions.

//
// Delegates in .Net 3.0
AddNumberDelegate Delegate_v3 = (Number1, Number2) => Number1 + Number2;
// Delegate Call
Response.Write(".Net 3.0 Delegate : " + Delegate_v3(1, 2));
Response.Write("
"
);

As you could see, the number of lines of code requires to do delegate programming is coming down with the improvements in the .Net Framework versions

All the three versions produce the same output
.Net 1.1 Delegate: 3
.Net 2.0 Delegate: 3
.Net 3.0 Delegate: 3

That’s it, we have seen the evolution of Delegate programming across various version of the .Net Framework.

Search Flipkart Products:
Flipkart.com

No comments: