C#
  Home arrow C# arrow C# Events Explained
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C#

C# Events Explained
By: Michael Youssef
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 48
    2007-02-13

    Table of Contents:
  • C# Events Explained
  • The Windows Form Example
  • The Steps of Creating an Event
  • The Bookshop Class and the AddBookEventHandler Delegate
  • The Notifier and the Sys Classes
  • Event Without an Event

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    C# Events Explained


    (Page 1 of 6 )

    It's time to discuss events in C#, since we had a good simple discussion about C# delegates in the past article. Today we explain what events are, how they are implemented, why they are exist and when to use them.

    I will show you how to create something that is event-like without an event and why it's a bad practice; I show you this technique to help you understand the design of .NET events. Also we will look at the conventions of naming our classes and methods that form the event model. If you are not familiar with delegates I suggest reading my article "C# Delegates Explained" because the concepts and techniques discussed here are based on that article.

    As you may remember from the discussion in the previous article, C# Delegates Explained, a delegate is an object that refers to a method, and we call this method by invoking the delegate itself. If you think about this for a minute you will realize that the event model is based on delegates. You must be familiar with events. An event is simply a notification of something that took place, such as a Windows form being loaded, a mouse button being clicked, a book being added to the book shop and other numerous behaviors. 

    We need a way to take some actions when an event is fired (or we can say took place) and this is introduced in C# through the use of delegates and event handler methods as we are going to see shortly. The event model is designed so that an object fires an event and another object (or objects) watches the occurrence of this event and calls a method to perform the action needed. The object that fires the event is called the publisher of the event and the object that has the method, which is called an event handler method, that is called when the event takes place is called the watcher object.

    How do delegates fit into the game? The publisher object doesn't know which event handler method of which watcher object needs to be called when the event takes place. Through the use of delegates any object (such as the watcher object) that needs to be notified about the occurrence of the event will have to create a new instance of a delegate (the type of the delegate is discussed later) that refers to the event handler method, then assigns (thus subscribing) this delegate to the publisher object's event. The event maintains a multicast delegate, and when the event takes place the multicast delegate invokes its invocation list of delegates, causing all the subscriber objects' (the watcher objects') event handler methods to be called.

    I know that you may be confused about how it's done, but by the end of this article you will understand the usefulness of delegates and events together. Let's begin with a very simple example, a Windows Form example.

    More C# Articles
    More By Michael Youssef


       · I work with C# for 9 months now and I couldn't understand events until I read your...
       · I always liked Michael intuitive writing style about various .NET topics and this...
       · Thanks for your comments about my article. Actually, each and every one of us who...
       · Mr.Michael I haven't read any of your articles and my first one was C# events. I'm...
       · I'm happy that you liked my articles and yes I plan to write about ASP.NET 2.0 very...
       · I work with different programming languages, and never meet such understanddable...
       · Glad you liked my articles. Could you please tell me what are those topics you want...
       · First, fantastic article. The best explanation of events that I have read to...
       · Hi,Thanks for your comment, I tried to do my best explaining those basics to...
       · Thanks Michael,That is what I was asking. I did not realise that there would...
       · You are welcome
       · Excellent article as well as the article on delegates which is a must read. Can you...
       · Thanks, I hope I can write more articles about C# soon. Anyway, I'm writing more...
       · You might also want to read my articles about C# Properties and Indexers. You will...
       · That was a good read...
       · one of the most clearly explained articles I have read this year. Thank you very...
       · Hi all,Thank you so much for your posts, .NET Windows Forms technology is based...
     

    C# ARTICLES

    - Coding a CRC-Generating Algorithm in C
    - Cyclic Redundancy Check
    - Handling Methods and Functions
    - Destroying Objects in C#
    - Creating Objects in C-Sharp
    - Classes and Objects
    - Programming Languages: Managed versus Native
    - LINQ-to-MySQL with DbLinq in C#
    - Working with Dates and Times in C#
    - Generics, Dictionaries, and More
    - More About Generics
    - Working with C# Collections
    - Generics
    - C# and XML
    - Pointers and Arrays in C#





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek