Event

Microsoft .NET Framework, ASP.NET, Visual C# (CSharp, C Sharp, C-Sharp) Developer Training, Visual Studio


Jump to: navigation, search
Exam Prep. Guides
Exam 70-536 Study Guide

1. Types and collections

2. Process, threading,…
3. Embedding features
4. Serialization, I/O
5. .NET Security
6. Interop., reflection,…
7. Global., drawing, text

edit

Contents

Summary

An event is basically a list of delegates. Technically, it is a multicast delegate. However, the event keyword allows the compiler to encapsulate the delegate to control how it is accessed.

The object that has the event is called the publisher. The object that handles the event is called the subscriber. An event allows a publisher to call a subscriber's event handler without knowing anything about the subscriber.

For example, a Button object with a Click event can call all the ClickEventHandlers without knowing the class or method names which handle the event.

It is the subscriber's responsibility to subscribe to the event. The publisher simply raises the event at the appropraite time. Whenever the event is raised, each delegate that is in the event's list will be called.

Remarks

The event keyword encapsulates the event delegate. With it, the compiler only allows += and -= operations outside the class. Therefore, only the publisher can access the delegate object directly to call it or edit the list of delegates.

Code Examples

.Net Event Conventions:

Name delegates used for an event type with a suffix of "EventHandler"

Name custom EventArgs with a suffix of "EventArgs" and inherit from EventArgs

EventHandler delegates should always have two parameters: object sender, and ___EventArgs e


Example Names:

// Event Handler Delegate Declaration
public delegate void MouseClickedEventHandler( object sender, MouseClickedEventArgs e );
 
// Event Args Declaration
public class MouseClickedEventArgs : EventArgs
{
//...
}
 
// Event Declaration
public event MouseClickedEventHandler MouseClicked;


Declare a basic event

public event EventHandler Changed;

Declare a Mouse Clicked event

public event MouseClickEventHandler MouseClicked;

Subscribe to an event

public void Subscribe()
{
	// Subscribe to the event
	btnCancel.Click += new EventHandler( CancelClick );
}
 
// The event handler
void CancelClick( object sender, EventArgs e )
{
	//...
}

Declare a delegate and an event with custom EventArgs

// Declare a delegate inside the namespace
public delegate void StatusChangedEventHandler( object sender, StatusChangedEventArgs e );
 
// StatusChangedEventArgs is at the bottom of this example
 
// The event publisher
public class ClassWithStatus
{
	// Declare the event
	public event StatusChangedEventHandler StatusChanged;
}
 
// Declare the custom event args class
// It should inherit from EventArgs
public class StatusChangedEventArgs : EventArgs
{
	private readonly bool _isDone;
	
	public StatusChangedEventArgs( bool isDone )
	{
		_isDone = isDone;
	}
	
	public bool IsDone
	{
		get
		{
			return _isDone;
		}
	}
}

Declare an event that uses generic EventHandler with custom EventArgs

// The event publisher
public class TreeNode
{
	// Declare the event
	public event EventHandler<ExpandedEventArgs> Expanded;
}
 
// The custom event args class
public class ExpandedEventArgs : EventArgs
{
	private readonly TreeNode _expandingNode;
	
	public StatusChangedEventArgs( TreeNode expandingNode )
	{
		_expandingNode = expandingNode;
	}
	
	public TreeNode ExpandingNode
	{
		get
		{
			return _expandingNode;
		}
	}
}

See Also

Delegate


Personal tools