Thursday, 29 October 2009

Cool Generic Delegates in C#

Here are some cool generic delegates (.NET 3.5) I find useful in my day to day coding.

  • Action Delegate
    • Action
    • Action(T)
    • Action(T1, T2)
    • Action(T1, T2, T3)
    • Action(T1, T2, T3, T4)
  • Predicate Delegate
    • Predicate<T>(T obj)
  • Func Delegate
    • Func<T, TResult>
    • Func<T1, T2, T3, T4, TResult>
    • Func<T1, T2, T3, TResult>
    • Func<T1, T2, TResult>
    • Func<TResult>

ACTION DELEGATE

The Action delegate encapsulates a method that has no return value and accepts up to four parameters. Action alone represents no parameter, Action(T), Action(T1, T2) etc… represents the number of parameters.

Simple example: using Action to replace Console.WriteLine, because Console.WriteLine accepts a single string parameter, we can simply assign it to the Action<string> and call action(“ABC”) which is equivalent to the Console.WriteLine(“ABC”)

static void Main(string[] args)

{

Action<string> action = Console.WriteLine;

action("ABC");

Console.ReadLine();

}

Or you can pass the action as a parameter and use it as such, using lambda, which will print “stringA1” to the console:

static void Main(string[] args)

{

DoSomething((s, n) =>

{

Console.WriteLine(s + n);

});

Console.ReadLine();

}

private static void DoSomething(Action<string, int> action)

{

string rawString = "stringA";

int rawNumber = 1;

action(rawString, rawNumber);

}

PREDICATE DELEGATE

The Predicate(T) delegate encapsulates a method that returns a Boolean value and accepts a single parameter.

A simple example:

static void Main(string[] args)

{

Predicate<byte> predicate = bit =>

{

return bit == 1 ? true : false;

};

// This will return true

Console.WriteLine(predicate(1));

// This will return false

Console.WriteLine(predicate(0));

Console.ReadLine();

}

In fact, most LINQ Queries uses this predicate delegate, one simple example is the .Find method:

static void Main(string[] args)

{

var list = new List<string>()

{

"a",

"b"

};

string a = list.Find(i => i == "a");

Console.WriteLine(a);

Console.ReadLine();

}

Another useful usage is in LINQ statements like so:

var list = new List<int>()

{

1,

2

};

Predicate<int> p = val => val == 1;

var i = from item in list

where p(item)

select item;

FUNC DELEGATE

The Func delegate is the most powerful of the lot. A Func can return any type (TResult) and accepts zero or up to four input parameters.

var list = new List<string>

{

"one",

"two",

"three"

};

// This indicates a return type of boolean, and accepts

// two input parameters of IEnumerable<string> and string

// respectively.

Func<IEnumerable<string>, string, bool>

funcPredicate = (collection, item) =>

{

if (collection != null && collection.Count() > 6)

{

return collection.Contains("one");

}

return false;

};

var i = from item in list

where funcPredicate (list, item)

select item;

There you have it, the useful delegates you will definitely find handy!

Other delegates I will cover in a later post will include Expression<T>, EventHandler<TEventArgs>, Converter<TInput, TOutput> and Comparison<T>.

Have fun coding!

No comments: