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 679295
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T01:14:39+00:00 2026-05-14T01:14:39+00:00

I wish to create own events and dispatch them. I never done this before

  • 0

I wish to create own events and dispatch them.
I never done this before in C#, only in Flex.. I guess there must be a lot of differencies.

Can anyone provide me a good example?

  • 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-14T01:14:39+00:00Added an answer on May 14, 2026 at 1:14 am

    There is a pattern that is used in all library classes. It is recommended for your own classes too, especially for framework/library code. But nobody will stop you when you deviate or skip a few steps.

    Here is a schematic based on the simplest event-delegate, System.Eventhandler.

    // The delegate type. This one is already defined in the library, in the System namespace
    // the `void (object, EventArgs)` signature is also the recommended pattern
    public delegate void Eventhandler(object sender, Eventargs args);  
    
    // your publishing class
    class Foo  
    {
        public event EventHandler Changed;    // the Event
    
        protected virtual void OnChanged()    // the Trigger method, called to raise the event
        {
            // make a copy to be more thread-safe
            EventHandler handler = Changed;   
    
            if (handler != null)
            {
                // invoke the subscribed event-handler(s)
                handler(this, EventArgs.Empty);  
            }
        }
    
        // an example of raising the event
        void SomeMethod()
        {
           if (...)        // on some condition
             OnChanged();  // raise the event
        }
    }
    

    And how to use it:

    // your subscribing class
    class Bar
    {       
        public Bar()
        {
            Foo f = new Foo();
            f.Changed += Foo_Changed;    // Subscribe, using the short notation
        }
    
        // the handler must conform to the signature
        void Foo_Changed(object sender, EventArgs args)  // the Handler (reacts)
        {
            // the things Bar has to do when Foo changes
        }
    }
    

    And when you have information to pass along:

    class MyEventArgs : EventArgs    // guideline: derive from EventArgs
    {
        public string Info { get; set; }
    }
    
    class Foo  
    {
        public event EventHandler<MyEventArgs> Changed;    // the Event
        ...
        protected virtual void OnChanged(string info)      // the Trigger
        {
            EventHandler handler = Changed;   // make a copy to be more thread-safe
            if (handler != null)
            {
               var args = new MyEventArgs(){Info = info};  // this part will vary
               handler(this, args);  
            }
        }
    }
    
    class Bar
    {       
       void Foo_Changed(object sender, MyEventArgs args)  // the Handler
       {
           string s = args.Info;
           ...
       }
    }
    

    Update

    Starting with C# 6 the calling code in the ‘Trigger’ method has become a lot easier, the null test can be shortened with the null-conditional operator ?. without making a copy while keeping thread-safety:

    protected virtual void OnChanged(string info)   // the Trigger
    {
        var args = new MyEventArgs{Info = info};    // this part will vary
        Changed?.Invoke(this, args);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I mean this quite literally. A close relative wants to create her own website
Before answering this question, understand that I am not asking how to create my
I have this problem, I created my own datagridviewcolumn and I wish add some
I wish to create a click-able object on a tag with javascript/jQuery. This obviously
I wish to create a very simple web app that simply displays an Image(s)
I am using WiX to create an installer, and wish to share a folder
I wish Subversion had a better way of moving tags. The only way that
Using Windows7 speech recognition I wish to create specialised vocabularies for recognising a domain-specific
I wish to create a constraint that state as below Code.CodeTable ( CodeID smallint,
i wish to create a repository pattern but with a WCF Rest Service which

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.