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

  • Home
  • SEARCH
  • 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 469453
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T23:47:03+00:00 2026-05-12T23:47:03+00:00

I need a very simple example to understand myself about the events.I am feeling

  • 0

I need a very simple example to understand myself about the events.I am feeling very difficult to understand the examples available in internet or books.

  • 1 1 Answer
  • 0 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-12T23:47:03+00:00Added an answer on May 12, 2026 at 11:47 pm

    This is a simple implementation of a class that exposes an event.

    public class ChangeNotifier
    {
        // Local data
        private int num; 
    
        // Ctor to assign data
        public ChangeNotifier(int number) { this.num = number; } 
    
         // The event that can be subscribed to
        public event EventHandler NumberChanged;
    
        public int Number
        {
            get { return this.num; }
            set
            {
                // If the value has changed...
                if (this.num != value) 
                {
                    // Assign the new value to private storage
                    this.num = value;
    
                    // And raise the event
                    if (this.NumberChanged != null)
                        this.NumberChanged(this, EventArgs.Empty);
                }
            }
        }
    }
    

    This class may be used something like as follows:

    public void SomeMethod()
    {
        ChangeNotifier notifier = new ChangeNotifier(10);
    
        // Subscribe to the event and output the number when it fires.
        notifier.NumberChanged += (s, e) => Console.Writeline(notifier.Number.ToString());
    
        notifier.Number = 10; // Does nothing, this is the same value
        notifier.Number = 20; // Outputs "20" because the event is raised and the lambda runs.
    }
    

    Regarding control flow, execution flows into SomeMethod(). We create a new ChangeNotifier and thus call its constructor. This assigns the value of 10 to the private num member.

    We then subscribe to the event using the += syntax. This operator takes a delegate on the right hand side (in our case, that delegate is a lambda) and adds it to the collection of delegates on the event. This operation doesn’t execute any code that we’ve written in the ChangeNotifier. It can be customized through the add and remove methods on the event if you’d like, but there’s rarely a need to do that.

    Then we perform a couple simple operations on the Number property. First we assign 10, which runs the set method on the Number property with value = 10. But the num member is already valued at 10, so the initial conditional evaluates to false and nothing happens.

    Then we do the same thing with 20. This time the value is different, so we assign the new value to num and fire the event. First we verify that the event is not null. It’s null if nothing has subscribed to it. If it’s not null (ie, if something is subscribed to it), we fire it using the standard method/delegate syntax. we simply call the event with the event’s arguments. This will call all methods that have subscribed to the event, including our lambda that will perform a Console.WriteLine().


    Henrik has successfully nitpicked the potential race condition that exists if one thread can be in Number‘s setter while another thread is unsubscribing a listener. I don’t consider that a common case for someone who doesn’t yet understand how events work, but if you’re concerned about that possibility, modify these lines:

    if (this.NumberChanged != null)
        this.NumberChanged(this, EventArgs.Empty);
    

    to be something like this:

    var tmp = this.NumberChanged;
    if (tmp != null)
        tmp(this, EventArgs.Empty);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need a very simple and clear example of how to create an OCX
I need to be able to merge two (very simple) JavaScript objects at runtime.
I need to do a few very simple URL manipulations in Java. Like get
i have very simple problem. I need to create model, that represent element of
I cannot understand the Silverlight documentation on data binding. I seek a very simple
I need a very quick fix in a mod_rewrite expression for drupal we have
I need a very accurate way to time parts of my program. I could
Say I need some very special multiplication operator. It may be implemented in following
I need to send a very specific (non-standard) string to an FTP server: dir
Im very new to SQL but need to write a query to do the

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.