Click here to Skip to main content
15,888,984 members
Articles / Programming Languages / C# 5.0
Tip/Trick

Events for Confused Beginners

Rate me:
Please Sign up or sign in to vote.
4.25/5 (9 votes)
16 Mar 2015CPOL2 min read 13.6K   66   17   6
A quick introduction to events for absolute beginners

 

Introduction

If you are a beginner .NET developer and you have trouble working with events - like me -, then this tip is for you. After immersing myself in articles and videos that address events in .NET, I managed to summarize these articles in a way that helped me understand its structure and hopefully, it will help you as well.

Background

Event is the way used by .NET to notify subscribers about a change or action in the event owner class. As shown in the following graph, the event owner will raise the event without knowing (or caring) about the subscribers. Once raised, .NET will send the notification for each subscriber. The subscriber should create an event handle. Event handle is simply a function that will be run whenever the event is raised.

Image 1

Using the Code

The events in .NET are based on the delegate structure. Delegate is a pointer to one or more functions that will be called together as in the following snippet.

C#
//The function will write to the string to a file
static void WriteToFile(string s){
    File.Append(s); // a pseudo code
}

//Write to log
static void WriteToLog(string s){
    log.info(s);
}

delegate void writeDelegate(string x); //Define the delegate signature

//Our lovely main function
static void Main(string[] args){

//define a variable of type writeDelegate and hook it the WriteToFile function
//The function must have the same signature as the delegate 
//(return void and accept one parameter of type string in our case)
    writeDelegate x = WriteToFile;
    x+= WriteToLog; //Hook the second function
    x+= (a) => Console.WriteLine(a); //We can also use lambda expression 
    
    //This line will call the 3 functions
    x("Hello World!");
}

So the old way to define an event is by creating a delegate, then define an event based on this delegate and finally raise the event. The following code raises event if the salary is more than 1000.

C#
class Person{
     public delegate void SalaryLimit(int s); //Define the delegate
     public event SalaryLimit IamRich; //Define the event

     private int salary;
     public int Salary{
           get {return salary;}
           set {
                   salary = value;
                   if (salary > 1000){
                         if (IamRich != null){ //an exception will be raised if there are no subscribers
                              IamRich(salary);
                         }
                   }
               }
     }
}

Instead of defining a custom delegate, we can directly use EventHandler and EventHandler<TEventArgs>, These are two predefined delegates with a slightly different signature that can be used with events. The first is usually used if there is no data to pass with the event as follows:

C#
class Person{
public event EventHandler TimeToSleep;
if (time is late){
      if (TimeToSleep != null) {
               TimeToSleep(this, Eventargs.Empty)
       }
}
}

If we want to pass data to subscribers along the event, we have to use EventHandler<TEventArgs> after creating a new class that inherits from EventArgs and add your parameter.

C#
class  AddressEventArgs: EventArgs {
         public string Address {get; set;}
}

Now define your event as follows:

C#
class Person{
       public event EventHandler<AddressEventArgs> AddressChange;
       private string address;
       public string Address{
                  get { return address;}
                  set { address = value;
                                         AddressEventArgs e = new AddressEventArgs();
                                         e.Address = address;
//Instead of calling the event directly, I will use the protected virtual onEvent method 
//as per MSDN guide
                                         onAddressChange(e);
                        }
                       }
                        
           protected virtual void onAddressChange(AddressEventArgs e){
                       EventHandler<AddressEventArgs> handler = AddressChange;
                       if (handler != null){
                             handler(this, e);
                      }
             }
}

On the other hand, the subscribers have to create an event handle which might be a function or a lambda expression. This handle will be called whenever the event is raised.

C#
static void Main(string[] args){
        Person p = new Person();
        // using lambda expression, C# is smart enough to infer the type of o and e;
        p.AddressChange += (o, e) => Console.WriteLine("My new address is " + e.Address);
        // using a function
        p.AddressChange += p_addressChange;
}

static void p_addressChange(object o, AddressEventArgs e){
        Console.WriteLine("My new address is " + e.Address);

}

Conclusion

This is my first tip here, hopefully I will get constructive feedback to enhance it. Thanks for your time.

History

  • March 15, 2015: Initial version
  • March 20, 2015: add the source code and fix some spell errors

 

License

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


Written By
Engineer
Canada Canada
Programming for me is a hobby

Comments and Discussions

 
QuestionGood Start! Pin
Your Display Name Here19-Mar-15 4:44
Your Display Name Here19-Mar-15 4:44 
AnswerRe: Good Start! Pin
Mostafa A. Ali20-Mar-15 5:00
professionalMostafa A. Ali20-Mar-15 5:00 
SuggestionGreat post Pin
Yang Li18-Mar-15 1:59
Yang Li18-Mar-15 1:59 
GeneralRe: Great post Pin
Mostafa A. Ali20-Mar-15 4:56
professionalMostafa A. Ali20-Mar-15 4:56 
QuestionGood One Pin
Vijay_Prasad17-Mar-15 17:35
Vijay_Prasad17-Mar-15 17:35 
AnswerRe: Good One Pin
Mostafa A. Ali20-Mar-15 4:55
professionalMostafa A. Ali20-Mar-15 4:55 

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.