First, I was reading some forums and the help in MSDN and all says that a delegate can't be overloaded.
Now, I want to have something like this:
public delegate void OneDelegate();
public delegate void OneDelegate(params object[] a);
public void DoNothing(params object[] a) {}
public void DoSomething() { /* do something */ }
private OneDelegate someFunction;
someFunction = new OneDelegate(DoSomething);
someFunction = new OneDelegate(DoNothing);
So, like you know, you CAN'T do this, because OneDelegate only refers to the first one and not the second one. But, is there a way for doing this? or something like that?
PS1: I want to have any number of OneDelegate declarations, not just one or two.
The Action class "does this". It's a delegate with templates, so you can have a delegate like this:
This is probably as close as you are going to get, i.e. you need a template type as a parameter.
Edit: Based on comments I guess you are after passing a delegate to another function. You can accomplish it by passing along the arguments as well. Unfortunately you cannot do this without the use of a params parameter to
fire
.Imagine for a moment this was possible. Suppose I could have an overloaded delegate:
Now imagine I declare a variable of this type and then assign a function to it, for example:
where
StringMethod
is declared thusly:Now you pass
myDelegate
to some other code, and that code does this:What do you expect to happen in this case? How can the runtime call
StringMethod()
with an integer argument?If you really want a delegate that can take any set of parameters at all, then the only option is to have one with a
params object[]
array:But then you will have to assign to it a function that can actually handle any object array, for example:
As you can see, this gets messy real quick. Therefore, I submit to you that if you need such a delegate, you have probably made a mistake somewhere in your architectural design. Identify this flaw and fix the design before you proceed with the implementation, as otherwise the maintainability of your code will suffer.