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.
C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...
In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr. Stroustroupe.
C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.
C is more difficult to learn than JavaScript, but it's a valuable skill to have because most programming languages are actually implemented in C. This is because C is a “machine-level” language. So learning it will teach you how a computer works and will actually make learning new languages in the future easier.
Imagine for a moment this was possible. Suppose I could have an overloaded delegate:
public delegate void OneDelegate(int i); public delegate void OneDelegate(string s);
Now imagine I declare a variable of this type and then assign a function to it, for example:
OneDelegate myDelegate = StringMethod;
where StringMethod
is declared thusly:
public void StringMethod(string s) { Console.WriteLine(s); }
Now you pass myDelegate
to some other code, and that code does this:
myDelegate(47);
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:
public delegate void OneDelegate(params object[] parameters);
But then you will have to assign to it a function that can actually handle any object array, for example:
public void MyMethod(params object[] parameters) { if (parameters == null || parameters.Length == 0) throw new ArgumentException("No parameters specified."); if (parameters.Length > 1) throw new ArgumentException("Too many parameters specified."); if (parameters[0] is int) IntMethod((int) parameters[0]); else if (parameters[0] is string) StringMethod((string) parameters[0]); else throw new ArgumentException("Unsupported parameter type."); }
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With