Click here to Skip to main content
15,919,479 members
Articles / Programming Languages / C#
Tip/Trick

Raising an event in C#

Rate me:
Please Sign up or sign in to vote.
2.05/5 (5 votes)
16 Aug 2013CPOL 85.2K   10   3
Raising an event in C#, methods and Critics

Introduction

During this years, I come across many habits of developers regarding raising an Event, In the present Post I will try to summarize all those ways and give some critics.

Code

  1. The first and intuitive way is to declare and use the event directly like in the example below :
  2. C#
    public class Foo
    {
      public event EventHandler FooEvent;
      public Foo()
      {
      }
      public void Execute()
      {
        //Do the work
        if (FooEvent != null)
          FooEvent(this, new EventArgs());
        //Continue the work
      }
    } 

    The problem with this is you have to test if the event is not null. and you might forget about it easily.

  3. The second approach is to create a function that raise the event for you:
    C#
    public class Foo
    {
      public event EventHandler FooEvent;
      public Foo()
      {
    
      }
      protected void OnFooEvent()
      {
        if (FooEvent == null)
          return;
        FooEvent(this, new EventArgs());
      }
      public void Execute()
      {
        //Do the work
        OnFooEvent();
        //Continue the work
      }
    }     

    Although this simplify the way we call the event but we are always able to call the event directly and so we may have a NullReferenceException. also we should define a function that raises the event in every class.

  4. The third way is a good way to be sure that we won't have a NullReferenceException.
    C#
    public class Foo
    {
      public event EventHandler FooEvent = new EventHandler((e, a) => { });
      public Foo()
      {
        
      }      
      public void Execute()
      {
        //Do the work
        FooEvent(this, new EventArgs());
        //Continue the work
      }
    }  

    With this way, we will never have a risk of NullReferenceException.

  5. The forth way is to create an extension function that raises the event and use it.
  6. C#
    public static class EventHelper
    {
      public static void Raise(this EventHandler eventHandler, object sender,EventArgs args)
      {
        if (eventHandler == null)
          return;
        eventHandler(sender, args);
      }
    }
    
    public class Foo
    {
      public event EventHandler FooEvent;
      public Foo()
      {
        
      }      
      public void Execute()
      {
        //Do the work
        FooEvent.Raise(this, new EventArgs());
        //Continue the work
      }
    }

The advantages of this way is the readability, also the null check is centralized.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
France France
Software developer engineer, Interested in .Net, C#, Computer security, Image processing and more.

Comments and Discussions

 
GeneralMy vote of 2 Pin
Paulo Zemek16-Aug-13 11:36
Paulo Zemek16-Aug-13 11:36 
GeneralMy vote of 1 Pin
OriginalGriff16-Aug-13 9:02
mveOriginalGriff16-Aug-13 9:02 
QuestionThread safe Pin
Ron Beyer16-Aug-13 6:30
professionalRon Beyer16-Aug-13 6:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.