Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 242659
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T20:51:22+00:00 2026-05-11T20:51:22+00:00

I am studying events in C# but there are not much articles or information

  • 0

I am studying events in C# but there are not much articles or information that show me where or what kinda position I’d need to use events in.

Could some one give me real world example that makes them more understandable.

Thanks in advance.

  • 1 1 Answer
  • 2 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T20:51:22+00:00Added an answer on May 11, 2026 at 8:51 pm

    As Chris Gray said, one use is to signal when something has happened that your code didn’t directly call. The most common cause here is probably user actions on the GUI. Another example might be an asynchronous operation completing on another thread.

    The other reason to use events is when you don’t know who might be interested in what has just happened. The class raising the event doesn’t need to know (at design time) anything about how many instances of what other classes might be interested.

    class Raiser {
    
       public DoSomething() {
          //Do something long winded.
          OnDidSomething(new DidSomethingEventArgs());
       }
    
       public EventHandler<DidSomethingEventArgs> DidSomething;
    
       private OnDidSomething(DidSomethingEventArgs e) {
          if (DidSomething != null)
             DidSomething(this, e);
       }
    }
    

    Obviously, you also need to define the DidSomethingEventArgs class which passes on the relevant data about the event. This also illustrates a common naming convention for events. If the event is called X, then the event is only ever raised in a method called OnX and any data it passes on is an instance of class XEventArgs. Note that an event can be null if no listeners are subscribed to it, hence the check just before we raise the event.

    Note that this class knows nothing about what other classes might be interested in the fact that it did something. It simply announces the fact that it has done it.

    Multiple classes can then listen out for the event:

    class ListenerA {
       private Raiser r;
    
       ListenerA(Raiser r) {
          this.r = r;
          r.DidSomething += R_DidSomething;
       }
    
       R_DidSomething(object sender, DidSomethingEventArgs e) {
          //Do something with the result.
       }
    }
    

    And:

    class ListenerB {
    
       private Raiser r;
    
       ListenerB(Raiser r) {
          this.r = r;
          r.DidSomething += R_DidSomething;
       }
    
       R_DidSomething(object sender, DidSomethingEventArgs e) {
          //Do something with the result.
       }
    }
    

    Now, when the DoSomething method is called on the Raiser instance, all instances of ListenerA and ListenerB will be informed via the DidSomething event. Note that the listener classes could easily be in different assemblies to the raiser. They need a reference back to the raiser’s assembly but it doesn’t need a reference to its listeners’ assemblies.

    Note that the above simple Raiser example may cause you some problems in a multi-threaded program. A more robust example would use something like:

    class Raiser {
    
       public DoSomething() {
          //Do something long winded.
          OnDidSomething(new DidSomethingEventArgs());
       }
    
       #region DidSomething Event
    
       private object _DidSomethingLock = new object();
       private EventHandler<DidSomethingEventArgs> _DidSomething;
    
       public EventHandler<DidSomethingEventArgs> DidSomething {
          add { lock(_DidSomethinglock) _DidSomething += value; }
          remove { lock(_DidSomethinglock) _DidSomething -= value; }
       }
    
       OnDidSomething(DidSomethingEventArgs e) {
          EventHandler<DidSomethingEventArgs> handler;
          lock (_DidSomethingLock)
             handler = _DidSomething;
          if (handler == null)
             return;
          try {
             DidSomething(this, e);
          } catch (Exception ex) {
             //Do something with the exception
          }
       }
    
       #endregion
    }
    

    This ensures that another thread adding or removing a listener while you are in the middle of raising the event doesn’t cause problems.

    The simple listeners used here will also cause memory leaks if instances of the listener classes are being created and destroyed. This is because the Raiser instance gets passed (and stores) a reference to each listener as they subscribe to the event. This is enough to prevent the garbage collector from properly tidying up the listeners when all explicit references to them are removed. The best way round this is probably to make the listeners implement the IDisposable interface and to unsubscribe from the events in the Dispose method. Then you just need to remember to call the Dispose method.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i studying TDateTime functions and procedure, but not found something that allow me to
while studying some security things, there was a question that one can guess the
when studying RMI sometimes (head first Java) dudes use Naming.rebind(name, object) but other peoples
Studying compilers course, I am left wondering why use registers at all. It is
After studying TCP/UDP difference all week, I just can't decide which to use. I
While studying for the Zend PHP Exam I came across the following contradicting information:
While studying the Collection API, we find that some methods ( add , remove
Studying STL I have tried to negate a functor with not2 but encontered problems.
I've just recently been studying JQuery to use on a personal website. Something I
I'm studying C# Events on this link and am a little lost on when

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.