Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# event subscribe and unsubscribe duplicates

Is there a problem if I subscribe to the same event three times with the eventHandler? e.g.

a.SomethingChanged += new EventHandler(ChangeHandler);
a.SomethingChanged += new EventHandler(ChangeHandler);
a.SomethingChanged += new EventHandler(ChangeHandler);

Does this cause ChangeHandler to be invoked 3 times instead of 1? What is best way to handle this?

Note that these redundancies are not together but different areas of code paths.

Similary, is there an issue with unsubscribing from an event that is not registered? e.g.

a.SomethingChanged -= new EventHandler(ChangeHandler);  //ChangeHandler was never registered
like image 998
user236215 Avatar asked Jan 22 '13 19:01

user236215


People also ask

What C is used for?

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 ...

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.

What is C full form?

Full form of C is “COMPILE”. One thing which was missing in C language was further added to C++ that is 'the concept of CLASSES'.


2 Answers

If you subscribe to an an event more than once then your handler will be called the corresponding number of times - in your example three.

Whether this is a problem or not depends on what your event handler does, but I'm assuming that you don't want it to be called multiple times.

There is no problem in unsubscribing from an event that you haven't subscribed to.

So if you're not sure what state your application is in (though you really should be) you can have:

a.SomethingChanged -= ChangeHandler;
...
a.SomethingChanged += ChangeHandler;

(Note: the new EventHandler(...) is syntactic sugar and can be omitted)

like image 58
ChrisF Avatar answered Oct 11 '22 00:10

ChrisF


Is there a problem if I subscribe to the same event three times with the eventHandler?

Nope, it will just add the event handler three times.

Does this cause ChangeHandler to be invoked 3 times instead of 1?

Yes.

What is best way to handle this?

That depends on what you want; which you haven't specified. If you want a way to add an event handler if and only if it hasn't already been added, then just remove the event handler and then add it again:

a.SomethingChanged -= new EventHandler(ChangeHandler);
a.SomethingChanged += new EventHandler(ChangeHandler);

Is there an issue with unsubscribing from an event that is not registered?

No, it will just do nothing.

like image 34
Servy Avatar answered Oct 10 '22 23:10

Servy