Implementing Interfaces

To implement an interface, you specify the interface name on the class or struct definition after the colon:

class Resource : IDisposable{    public void Dispose() { ... }}

When doing so, provide implementations for the interface’s members or mark them as abstract, deferring to a subclass. Two ways to implement interface members are available. The one shown in the preceding snippet is sometimes referred to as implicit interface implementation. When defining the Dispose method, we didn’t state we’re going to satisfy the contract provided by IDisposable. As a result, we can call the interface method in a variety of ways:

Resource res = new Resource();res.Dispose();((IDisposable)res).Dispose();

In the second case, we’re using ...

Get C# 5.0 Unleashed now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.