Home
 
  

Delegates, Anonymous methods and Lambda

March 8, 2014

Definition : A delegate is a type that references a method. Once a delegate is assigned a method, it behaves exactly like that method. The delegate method can be used like any other method, with parameters and a return value. (source : MSDN )

delegate is defined by delegate keyword lets understand with example I am creating delegate with name Math

delegate int Math(int value1, int value2);

in this delegate 2 int argument and int return type so we can assign any functions reference which takes 2 int arguments and returns int value, I created a method for sum two values.

public static int Add(int a, int b)

public static int Add(int a, int b)

{

return a + b;

}

I can assign reference of this method to Math delegate, like

Math math = Add;

and call this delegate

math(1, 1);

it return 2 because this delegate call Add method. We can also assign anonymous methods to delegate

Math math = delegate(int a, int b) { return a + b; };

above I created a anonymous method and assign to math delegate. we can also use lambda (=>) notation for creating anonymous functions.

Math math = (a, b) => { return a + b; };

We can also write if our anonymous method contains only one statement

Math math = (a, b) => a + b;

in above statement no need to write datatype, lambda notation widely used in C# these days for creating anonymous methods.

class Program

{

delegate int Math(int value1, int value2);

static void Main(string[] args)

{

Math math = (a, b) = >a + b;

Console.Write(math(1, 1));

}

public static int Add(int a, int b)

{

return a + b;

}

}

delegate allows to pass functions as arguments and its very powerful feature of .net with this we can create more generic method, lambda notation used for passing small methods to function.

Let understand a example in this I am passing methods reference to method for creating more generic method.

class Program
{
public delegate bool NameCriteria(string name);
static void Main(string[] args)
{
var namesstartwithm = GetNames(name = >name.StartsWith("M"));
Console.WriteLine("--- Name which are starts with M char ------");
foreach(var name in namesstartwithm)
{
Console.WriteLine(name);
}
var names = GetNames(name = >name.Count() <= 3);
Console.WriteLine("nn--- Name which are contains less then or equals to 3 chars ------");
foreach(var name in names)
{
Console.WriteLine(name);
}
Console.ReadLine();
}
public static IEnumerable < string > GetNames(NameCriteria namecriteria)
{
List < string > names = new List < string > ();
names.Add("John");
names.Add("Martin");
names.Add("Tom");
names.Add("Nick");
names.Add("Geoge");
names.Add("Matt");
foreach(var name in names.AsEnumerable())
{
if (namecriteria(name))
{
yield return name;
}
}
}
}
//output
//
//--- Name which are starts with M char ------
//Martin
//Matt
//--- Name which are contains less then or equals to 3 chars ------
//Tom

in above program I created a method GetNames (in this I create List of names for example) it returns list of names as IEnumerable and it takes one parameter NameCriteria (defined at first line of class Program) type, NameCriteria is a delegate this delegate takes one string parameter and return boolean value.In GetNames method I iterate the name list and pass name to the namecriteria delegate and check if it is true then return. (note yield used to return iterate values)

Then I used this method for two type

  1. in first I passed following anonymous method, it check is name starts with M

name => name.StartsWith("M")

  1. in second I passed following anonymous method, it check is name contains less then or equals 3 chars.

name => name.Count()<=3

Govind Malviya

Govind Malviya

Director of Product Development @LoginRadius

 

© 2020