www.pickatutorial.com Computer Tutorials
Top eBooks: C/C++ | C# | Android | Mathematics | Database | Cloud | Graphics | Networking | Oracle | Hardware | AI
Top Tutorials: C/C++ | C#.NET | PHP MySQL | Java | Java Script | jQuery | HTML | xHTML | HTML5 | VB Script| CSS
Lessons C# Events Bookmark and Share
Tutorial Home
Lesson 1
Lesson 2
Lesson 3
Lesson 4
Lesson 5
Lesson 6
Lesson 7
Lesson 8
Lesson 9
Lesson 10
Lesson 11
Lesson 12
Lesson 13
Lesson 14
Lesson 15
Lesson 16
Lesson 17
Lesson 18
Lesson 19
Lesson 20
Lesson 21
Lesson 22
Lesson 23
Events are used extensively in GUI programming. An event, in C#, is a special kind of delegate that facilitates event-driven programming. Events are class members, which we cannot call outside of the class regardless of its access specifier. In other words, an event declared as public in a class allows other classes to use += operator to add methods to the event. We can also use -= operator to remove methods form a delegate or an event. However, firing the event (i.e. invoking the delegate) is only allowed in the class containing the event.

Now consider the following example:
delegate void SimpleButtonClickHandler();
class MyButton
   public event SimpleButtonClickHandler MyButtonClicked; 
   public void SimulateButtonClick() 
      if (MyButtonClicked != null) 
A method in another class can then subscribe to the event by adding one of its methods to the event delegate:

MyButton b = new MyButton();
b.ButtonClicked += MyClickHandler;

Note that despite the fact that the event is declared as public, it cannot be directly fired from anywhere except in the class containing the event.
Our C# Programming Tutorial for Java and C++ Programmers ends here. How did you find our efforts? Please let us know at pickatutorial@yahoo.com | Tutorial author: farooq_fl@yahoo.com

More C# Programming Tutorials

Home - Advertise - Contact - Disclaimer - About Us
© Since 2006 pickatutorial.com -- All Rights Reserved.