Mar 29 2008

Ready-Made Delegates in .NET 2.0

Category:Bil@l @ 22:31

Sometimes while writing your code you need to create new delegates to meet your specific project requirements to call specific methods. Before directly opening a new CS or VB class and start adding new delegates, make sure to check the read-made delegates introduced by .NET 2.0. Some of these are listed here:

 

.NET 2.0 Read-Made Delegates

1. delegate Boolean Predicate<T>(T obj);

This is a delegate that can hold a reference to a method that takes as input a parameter of type T and returns boolean.

 

2. delegate void Action<T>(T obj);

This is a delegate that can hold a reference to a method that takes as input a parameter of type T and returns nothing.

 

3. delegate void MethodInvoker();

This is a delegate that can hold a reference to a method that takes no input parameters and returns nothing.

 

In .NET 3.5 new delegates were added:

delegate void Action();

delegate void Action<T1, T2>(T1 arg1, T2 arg2);

delegate void Action<T1, T2, T3>(T1 arg1, T2 arg2);

delegate void Action<T1, T2, T3, T4>(T1 arg1, T2 arg2,T3 arg3, T4 arg4);

delegate TResult Func<TResult>();

delegate TResult Func<T, TResult>(T arg);

delegate TResult Func<T1, T2, TResult>(T1 arg1, T2 arg2);

delegate TResult Func<T1, T2, T3, TResult>(T1 arg1, T2 arg2);

delegate TResult Func<T1, T2, T3, T4, TResult>(T1 arg1, T2 arg2, T3 arg3, T4 arg4);

 

The above delegates are self explained. So now, when you need a new delegate, check the existing ones before creating your own! The above delegates are all present inside the System namespace.

 

Hope this helps,
Regards

Tags:

Comments are closed