As you may have noticed in .NET Framework 3.5 the lonely System.Action<T>(T) delegate got some team mates:
- public delegate void Action()
- public delegate void Action<T1, T2>(T1, T2)
- public delegate void Action<T1, T2, T3>(T1, T2, T3)
- public delegate void Action<T1, T2, T3, T4>(T1, T2, T3, T4)
- public delegate TResult Func<TResult>()
- public delegate TResult Func<T, TResult>(T)
- public delegate TResult Func<T1, T2, TResult>(T1, T2)
- public delegate TResult Func<T1, T2, T3, TResult>(T1, T2, T3)
- public delegate TResult Func<T1, T2, T3, T4, TResult>(T1, T2, T3, T4)
These come quite handy if you need some multi-purpose delegates and don’t want to write them on your own.
But I like to backport some features if possible to older platforms because of .NET Framework 3.5 not supported on some of the old school Win2K systems. By defining the necessary delegates in the same namespace as the later framework you don’t have to change anything if you upgrade your target platform later.
Here comes the code:
namespace System
{
//// All these delegate are built-in .NET 3.5
//// Comment/Remove them when compiling to .NET 3.5 to avoid ambiguity.
using System;
using System.Collections.Generic;
using System.Text;
/// <summary>
/// Encapsulates a method that takes no parameters and does not return a value.
/// </summary>
/// <remarks>
/// <p>
/// You can use this delegate to pass a method that performs some operation as a parameter without explicitly declaring a custom delegate to encapsulate it.
/// The encapsulated method must correspond to the method signature defined by this delegate. This means that the method must have no parameters and no return value.
/// </p>
/// <note>
/// To reference a method that has no parameters and that returns a value, use the <see cref="Func<TResult>"/> delegate instead.
/// </note>
/// </remarks>
public delegate void Action();
/// <summary>
/// Encapsulates a method that has two parameters and does not return a value.
/// </summary>
/// <remarks>
/// <p>
/// You can use the <see cref="Action<T1, T2>"/> delegate to pass a method as a parameter without explicitly declaring a custom delegate.
/// The method must correspond to the method signature that is defined by this delegate. This means that the encapsulated method must have two parameters
/// that are both passed to it by value, and must not return a value. (In C#, the method must return void. In Visual Basic, it must be defined by the
/// Sub…End Sub construct.) Typically, such a method is used to perform an operation.
/// </p>
/// <note>
/// To reference a method that has two parameters and returns a value, use the generic <see cref="Func<T1, T2, TResult>"/> delegate instead.
/// </note>
/// </remarks>
/// <typeparam name="T1">The type of the first parameter of the method that this delegate encapsulates.</typeparam>
/// <typeparam name="T2">The type of the second parameter of the method that this delegate encapsulates.</typeparam>
/// <param name="arg1">The first parameter of the method that this delegate encapsulates.</param>
/// <param name="arg2">The second parameter of the method that this delegate encapsulates.</param>
public delegate void Action<T1, T2>(T1 arg1, T2 arg2);
/// <summary>
/// Encapsulates a method that takes three parameters and does not return a value.
/// </summary>
/// <remarks>
/// <p>
/// You can use the <see cref="Action<T1, T2, T3>"/> delegate to pass a method as a parameter without explicitly declaring a custom delegate.
/// The method must correspond to the method signature that is defined by this delegate. This means that the encapsulated method must have three parameters
/// that are all passed to it by value, and it must not return a value. (In C#, the method must return void. In Visual Basic, it must be defined by the
/// Sub…End Sub construct.) Typically, such a method is used to perform an operation.
/// </p>
/// <note>
/// To reference a method that has three parameters and returns a value, use the generic <see cref="Func<T1, T2, T3, TResult>"/> delegate instead.
/// </note>
/// </remarks>
/// <typeparam name="T1">The type of the first parameter of the method that this delegate encapsulates.</typeparam>
/// <typeparam name="T2">The type of the second parameter of the method that this delegate encapsulates.</typeparam>
/// <typeparam name="T3">The type of the third parameter of the method that this delegate encapsulates.</typeparam>
/// <param name="arg1">The first parameter of the method that this delegate encapsulates.</param>
/// <param name="arg2">The second parameter of the method that this delegate encapsulates.</param>
/// <param name="arg3">The third parameter of the method that this delegate encapsulates.</param>
public delegate void Action<T1, T2, T3>(T1 arg1, T2 arg2, T3 arg3);
/// <summary>
/// Encapsulates a method that has four parameters and does not return a value.
/// </summary>
/// <remarks>
/// <p>
/// You can use the <see cref="Action<T1, T2, T3, T4>"/> delegate to pass a method as a parameter without explicitly declaring a custom delegate.
/// The encapsulated method must correspond to the method signature that is defined by this delegate. This means that the encapsulated method must have four
/// parameters that are all passed to it by value, and it must not return a value. (In C#, the method must return void. In Visual Basic, it must be defined by
/// the Sub…End Sub construct.) Typically, such a method is used to perform an operation.
/// </p>
/// <note>
/// To reference a method that has four parameters and returns a value, use the generic <see cref="Func<T1, T2, T3, T4, TResult>"/> delegate instead.
/// </note>
/// </remarks>
/// <typeparam name="T1">The type of the first parameter of the method that this delegate encapsulates.</typeparam>
/// <typeparam name="T2">The type of the second parameter of the method that this delegate encapsulates.</typeparam>
/// <typeparam name="T3">The type of the third parameter of the method that this delegate encapsulates.</typeparam>
/// <typeparam name="T4">The type of the fourth parameter of the method that this delegate encapsulates.</typeparam>
/// <param name="arg1">The first parameter of the method that this delegate encapsulates.</param>
/// <param name="arg2">The second parameter of the method that this delegate encapsulates.</param>
/// <param name="arg3">The third parameter of the method that this delegate encapsulates.</param>
/// <param name="arg4">The fourth parameter of the method that this delegate encapsulates.</param>
public delegate void Action<T1, T2, T3, T4>(T1 arg1, T2 arg2, T3 arg3, T4 arg4);
}
And the Func delegates:
namespace System
{
//// All these delegate are built-in .NET 3.5
//// Comment/Remove them when compiling to .NET 3.5 to avoid ambiguity.
using System;
using System.Collections.Generic;
using System.Text;
/// <summary>
/// Encapsulates a method that has no parameters and returns a value of the type specified by the <typeparamref name="TResult"/> parameter.
/// </summary>
/// <remarks>
/// You can use this delegate to construct a method that can be passed as a parameter without explicitly declaring a custom delegate.
/// The method must correspond to the method signature that is defined by this delegate. This means that the encapsulated method must have no parameters and must return a value.
/// </remarks>
/// <typeparam name="TResult">The type of the return value of the method that this delegate encapsulates.</typeparam>
/// <returns>The return value of the method that this delegate encapsulates.</returns>
public delegate TResult Func<TResult>();
/// <summary>
/// Encapsulates a method that has one parameter and returns a value of the type specified by the <typeparamref name="TResult"/> parameter.
/// </summary>
/// <typeparam name="T">The type of the parameter of the method that this delegate encapsulates.</typeparam>
/// <typeparam name="TResult">The type of the return value of the method that this delegate encapsulates.</typeparam>
/// <param name="arg">The parameter of the method that this delegate encapsulates.</param>
/// <returns>The return value of the method that this delegate encapsulates.</returns>
public delegate TResult Func<T, TResult>(T arg);
/// <summary>
/// Encapsulates a method that has two parameters and returns a value of the type specified by the <typeparamref name="TResult"/> parameter.
/// </summary>
/// <typeparam name="T1">The type of the first parameter of the method that this delegate encapsulates.</typeparam>
/// <typeparam name="T2">The type of the second parameter of the method that this delegate encapsulates.</typeparam>
/// <typeparam name="TResult">The type of the return value of the method that this delegate encapsulates.</typeparam>
/// <param name="arg1">The first parameter of the method that this delegate encapsulates.</param>
/// <param name="arg2">The second parameter of the method that this delegate encapsulates.</param>
/// <returns>The return value of the method that this delegate encapsulates.</returns>
public delegate TResult Func<T1, T2, TResult>(T1 arg1, T2 arg2);
/// <summary>
/// Encapsulates a method that has three parameters and returns a value of the type specified by the <typeparamref name="TResult"/> parameter.
/// </summary>
/// <remarks>
/// <p>
/// You can use this delegate to construct a method that can be passed as a parameter without explicitly declaring a custom delegate.
/// The method must correspond to the method signature that is defined by this delegate. This means that the encapsulated method must have three parameters,
/// each of which is passed to it by value, and that it must return a value.
/// </p>
/// <note>
/// To reference a method that has three parameters and returns <b>void</b> (or in Visual Basic, that is declared as a Sub rather than as a Function),
/// use the generic <see cref="Action<T1, T2, T3>"/> delegate instead.
/// </note>
/// </remarks>
/// <typeparam name="T1">The type of the first parameter of the method that this delegate encapsulates.</typeparam>
/// <typeparam name="T2">The type of the second parameter of the method that this delegate encapsulates.</typeparam>
/// <typeparam name="T3">The type of the third parameter of the method that this delegate encapsulates.</typeparam>
/// <typeparam name="TResult">The type of the return value of the method that this delegate encapsulates.</typeparam>
/// <param name="arg1">The first parameter of the method that this delegate encapsulates.</param>
/// <param name="arg2">The second parameter of the method that this delegate encapsulates.</param>
/// <param name="arg3">The third parameter of the method that this delegate encapsulates.</param>
/// <returns>The return value of the method that this delegate encapsulates.</returns>
public delegate TResult Func<T1, T2, T3, TResult>(T1 arg1, T2 arg2, T3 arg3);
/// <summary>
/// Encapsulates a method that has four parameters and returns a value of the type specified by the <typeparamref name="TResult"/> parameter.
/// </summary>
/// <remarks>
/// <p>
/// You can use this delegate to construct a method that can be passed as a parameter without explicitly declaring a custom delegate.
/// The method must correspond to the method signature that is defined by this delegate. This means that the encapsulated method must have four parameters,
/// each of which is passed to it by value, and that it must return a value.
/// </p>
/// <note>
/// To reference a method that has four parameters and that returns void (or in Visual Basic, that is declared as a Sub rather than as a Function),
/// use the generic Action<T1, T2, T3, T4> delegate instead.
/// </note>
/// </remarks>
/// <typeparam name="T1">The type of the first parameter of the method that this delegate encapsulates.</typeparam>
/// <typeparam name="T2">The type of the second parameter of the method that this delegate encapsulates.</typeparam>
/// <typeparam name="T3">The type of the third parameter of the method that this delegate encapsulates.</typeparam>
/// <typeparam name="T4">The type of the fourth parameter of the method that this delegate encapsulates.</typeparam>
/// <typeparam name="TResult">The type of the return value of the method that this delegate encapsulates.</typeparam>
/// <param name="arg1">The first parameter of the method that this delegate encapsulates.</param>
/// <param name="arg2">The second parameter of the method that this delegate encapsulates.</param>
/// <param name="arg3">The third parameter of the method that this delegate encapsulates.</param>
/// <param name="arg4">The fourth parameter of the method that this delegate encapsulates.</param>
/// <returns>The return value of the method that this delegate encapsulates.</returns>
public delegate TResult Func<T1, T2, T3, T4, TResult>(T1 arg1, T2 arg2, T3 arg3, T4 arg4);
}




