Basically a delegate is a function signature.
public delegate void DelegateWithParameters(int a, int b);
Your delegate has a signature of a function that accepts 2 ints as parameters and returns void. In order to call a delegate of that type, you have to use two int parameters.
However, you could wrap that delegate in another method, so that you don't have to provide the parameters. Let's say I want to call a delegate above with a default set of parameters.
Now I could call that function from anywhere, without specifying parameters for the delegate. (Although they will always be necessary)
public void CallDelegate(DelegateWithParameters method)
{
method(1, 2);
}
public void Test(int a,int b)
{
// Do something
}
// Use it like so
CallDelegate(Test);
Or you could have a class with a field containing the delegate for example:
class DelegateInvoker
{
private DelegateWithParameters method;
public DelegateInvoker(DelegateWithParameters method)
{
this.method = method ?? throw new ArgumentNullException(nameof(method));
}
// Note this signature is parameterless
public void InvokeDelegate()
{
// but you do call the delegate with the required parameters
this.method(1, 2);
}
}
// Then call it like this
var delegateInvoker = new DelegateInvoker(Test);
delegateInvoker.InvokeDelegate();
A more inline approach would be to create a function on the fly, but it is basically the same thing. You define a new function to wrap the delegate.
DelegateWithParameters method = Test;
// define a new, parameterless method to wrap the delegate
var delegateInvoker = () => method(1, 2);
delegateInvoker();
Lastly, note that the newly created function actually has another signature. So you could define our new function as a delegate like this:
delegate void ParameterlessDelegate();
And the last example could become:
DelegateWithParameters method = Test;
// define a new, parameterless method to wrap the delegate
ParameterlessDelegate delegateInvoker = () => method(1, 2);
delegateInvoker();