miércoles, 27 de noviembre de 2013

Labeled as:

Delegates and Lambda Expressions... fearless

C# and Developing
Intended for: .NET medium-skilled developers

Delegates


Before we start this entry, we have to warn you about this is not a book so, we don't explain with details this theme. Its frequent to watch developers with fear to this concepts because are hard to understand even with a book in your hands. I've try to explain this in a simple way.


A deletate is a function definition to be used as a parameter to other one. Is as easy as the previous phrase explains it. Maybe you can find better explained definitions about delegates; but they could be more confusing than this.

public delegate bool DelegateFunction(int parameter1, int parameter2);

This is the way to declare a delegate, is enough. It is usefull as a "shell" to be passed as parameter another function with this kind of declaration: two integers as entry and a boolean as returned type.

Now I'm going to create a function that uses the delegate as parameter:

public string ExampleFunctionWithParameterDelegate(DelegateFunction df, int p1, int p2)
{
if (df(p1, p2))
{
return String.Format("Function condition accomplished df using {0} and {1}",
                    p1,
                    p2);
}
return String.Format("Function condition NOT accomplished df using {0} and {1}",
                    p1,
                    p2);
}

This drives us to understand how delegates works. Now we have to create functions that have the entry and return parameters as follows:

public bool DelegateExampleFunctionP1IsLessThanP2(int parameter1, int parameter2)
{
return parameter1 < parameter2;
}

public bool DelegateExampleFunctionP1IsMoreThanP2(int parameter1, int parameter2)
{
return parameter1 > parameter2;
}

Now, we have to connect everything with the following calls:

var p1 = 5;
var p2 = 2;
var returned1 = ExampleFunctionWithParameterDelegate(
DelegateExampleFunctionP1IsLessThanP2, p1, p2);
var returned2 = ExampleFunctionWithParameterDelegate(
DelegateExampleFunctionP1IsMoreThanP2, p1, p2);


Can you imagine what are the values for the variables returned1 and returned2 after the execution of the instructions? These values are the following ones:

returned1 = "Function condition NOT accomplished df using 5 and 2"
returned2 = "Function condition accomplished df using 5 and 2"

As we can see, this allows us to make flexible functions, even "mutants". Now I'm going to explain Lambda expressions, using the same example as delegates.

Lambda Expressions


Following the previous idea here is an example of Lambda expression that is usefull  to know how they work, making comparitions with the previous develop I've mark in yellow Lambda expressions:


var returned1 = ExampleFunctionWithParameterDelegate(
DelegateExampleFunctionP1IsLessThanP2, p1, p2);
var lambdaReturned1 = ExampleFunctionWithParameterDelegate(
(parameter1, parameter2) => { return parameter1 < parameter2; }, 
         p1, p2);

var returned2 = ExampleFunctionWithParameterDelegate(
DelegateExampleFunctionP1IsMoreThanP2, p1, p2);
var lambdaReturned2 = ExampleFunctionWithParameterDelegate(
(parameter1, parameter2) => { return parameter1 > parameter2; },
p1, p2);


We observe that the difference between the previous delegates code and now is that now we don't need the auxiliary created functions, I've made it "on the fly" and without direct define inside the call parameters of the call of main function with the delegate.

As you can imagine, the following is the result that is the same than the previous one:

returned1 = "Function condition NOT accomplished df using 5 and 2"
lambdaReturned1 = "Function condition NOT accomplished df using 5 and 2"
returned2 = "Function condition accomplished df using 5 and 2"
lambdaReturned2 = "Function condition accomplished df using 5 and 2"

Lambda expressions and delegates are implemented in several functionallities inside the framework elements. One of the most frequent places to be founded is inside the objects of System.Collections.Generic class, for example inside the lists and it will be usefull to be used in the querys over that collections.

Unknown

Author y editor

0 comentarios:

Publicar un comentario

 

Friendly Websites:

  • Manuel Enrique Díaz Rodríguez
  • CuRadio
  • Copyright © DotNet Pathways 2015
    Distributed By My Blogger Themes | Designed By Templateism