Can't overload Delegate for Custom Event?
i want to create
public delegate void ValueChangedHandler(int value);
public dele开发者_如何学Gogate void ValueChangedHandler(Object sender, int value);
it refuses: how to do so or is it impossible ?
EDIT: Thanks for the answer, I understand the technical reason, but still what I want to do makes sense from expressivity point of view so I'm upset that .net framework doesn't have a way to do so.
You're confusing delegates and methods... Methods can be overridden, delegates can't.
A delegate is a type, you can declare a variable of type ValueChangedHandler
:
ValueChangedHandler handler;
If you could overload delegates, to which overload of ValueChangedHandler
would this code refer ?
Overloading delegates just wouldn't make sense...
A delegate is a type. i.e if you are creating a delegate then you are actually making a class which is derived from System.Delegate.
public delegate void ValueChangedHandler(int value);
So by this you have created a class ValueChangedHandler. So again if you are writing
public delegate void ValueChangedHandler(int value, int j);
then it is two classes with same name under a single namespace. So the compiler will not allow.
I'm not sure that you can overload delegates, never seen it before.
What I would suggest would be something like this :
public delegate void ValueChangedHandler(params object[] list);
Hope this helps,
Since ValueChangedHandler is not a method, but a type, you can't overload it - you can instead create a new event handler with different parameters.
The common practice is also to extend paraterers of an event handler:
public delegate void EventHandler(object sender, EventArgs e);
You can define your custom class extending EventArgs to pass more data.
精彩评论