I do need some delegates from time to time (like when using threads in a WinForms App). But I’m too lazy to declare all needed delegates, especially if they have none or only very few parameters.

Luckily, there are some predefined delegates which can be used for that.

Here’s a list of some of those:

EventHandler // Default event callbacks
EventHandler<T> // Default event callbacks with custom parameter (inheriting from EventArgs)
Action<T1, T2, T3, T4> // Function without return value and 0-4 parameters
Func<T1, T2, T3, T4, TResult> // Methos with 0-4 parameters and one result type
Predicate<T> // equivalent to Func<T, bool>

And here’s a very simple example:

private void InvokedClose()
{
	if (InvokeRequired)
	{
		Invoke(new Action(InvokedClose));
	}
	else
	{
		this.Close();
	}
}

Leave a Reply

Your email address will not be published.

*