C# Interface Events
C# Interface Events
C# Interface Events
C# Interface Events
by Richard Carr, published at http://www.blackwasp.co.uk/CSharpInterfaceEvents.aspx
.NET 1.1+ .NET Framework
An interface can be created to define a contract containing members that classes that Algorithms and Data Structures
implement it must provide. Interfaces can define events, sometimes leading to classes that Audio
implement several interfaces being required to declare an event name twice. C# Programming
Configuration
Debugging
Declaring Events in Interfaces Design Patterns
Documentation
In a previous article that formed part of the C# Object-Oriented Programming tutorial, I Graphics
described the use of interfaces, which can be used to create contracts for classes. In that Input / Output
article were examples of interfaces that enforced the inclusion of methods and properties in LINQ
the classes that implemented them. Interfaces are not limited to defining methods and Network and Internet
In this article we will look at how events can be included in interfaces and how they are added Programming Concepts
Refactoring
to classes using implicit or explicit implementation. We will also examine the rare but
Reference Sheets
important situation where a class implements two interfaces that each contain an event with
Reflection
the same name.
Regular Expressions
Security
De ning an Interface Event SQL Server
System Information
To demonstrate the use of events within interfaces we need an interface to work with. To Testing
follow the examples, create a new console application and add the following interface to the Tools
project. This interface includes an event named, "Notify". The NotifyNow method is an artificial Visual Studio
item that we will use to force the event to be raised. Windows Programming
Windows Presentation
public interface INotify Foundation
{
XML
event EventHandler Notify;
void NotifyNow();
}
©2006-2019 BlackWasp
All Rights Reserved
Cookie Policy
Implementing an Interface Event Privacy Policy
Terms of Use
We can now create a class that implements the INotify interface. The Test class defined below
achieves this by declaring the event as a public member. The public NotifyNow method simply
calls the private OnNotify method, which in turn raises the event if there are any subscribers.
The syntax of the event and the OnNotify method that raises it are no di erent to that of an
event that is not defined in an interface.
public class Test : INotify
{
public event EventHandler Notify;
To test that the event is functioning correctly, add the following code, replacing the Main
method of the console application, and run the program. The updated Main method creates a
new Test object as a variable of the INotify type. It subscribes to the event and then calls the
Notify method to force the event to be raised. When the event is raised, the subscribed Notified
method is executed and a message is outputted to the console.
static void Main()
{
INotify test = new Test();
test.Notify += new EventHandler(Notified);
test.NotifyNow(); // Outputs "Notified"
}
void INotify.NotifyNow()
{
OnNotify(new EventArgs());
}
void OnNotify(EventArgs e)
{
if (_notify != null) _notify(this, e);
}
}
Next: Page 2
24 September 2010