About This eBook by Bart De Smet

Safari, the world’s most comprehensive technology and business learning platform.

Find the exact information you need to solve a problem on the fly, or go deeper to master the technologies and skills you need to succeed

Start Free Trial

No credit card required

O'Reilly logo

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

Find the exact information you need to solve a problem on the fly, or go deeper to master the technologies and skills you need to succeed

Start Free Trial

No credit card required