O'Reilly logo

C# 5.0 Unleashed by Bart De Smet

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required