views:

50

answers:

3

Can we have same delegate for two events ? which have same number of input parameters? or delegate and the events have one to one relation ?

+1  A: 

Sure, many events use the EventHandler as simple delegate.

Or do you mean, that you can subscribe many events to the same Method? That's possible too, you can subscribe e.g. subscribe TextChanged-Events from all Textboxes to the same Delegate. Especially for Validating and Validated events it is usefull to use only one method for all fields that utilize the same validating logic.

Events are generally multicast in .NET, so you can subscribe many delegates to one event. For example you can specify two or three validating methods for text fields, and subscribe the TextBox.Validating event to all methods that validate one aspect of the input.

BeowulfOF
A: 

As long as the event handlers are the same, you can have the same handler on as many events as you like.

From MSDN:

The delegate type defines the set of arguments that are passed to the method that handles the event. Multiple events can share the same delegate type, so this step is only necessary if no suitable delegate type has already been declared.

Kyle Rozendo
+1  A: 

Do you mean using the same delegate type for two different event declarations, or using the same delegate instance to subscribe to two different events? Both are allowed:

public event EventHandler Foo;
public event EventHandler Bar;
...
EventHandler handler = SomeMethod;
Foo += handler;
Bar += handler;
Jon Skeet