15.02.2015 Views

C# 4 and .NET 4

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

interfaces ❘ 101<br />

For example, take IDisposable . If a class wants to declare publicly that it implements the Dispose()<br />

method, it must implement IDisposable — which in <strong>C#</strong> terms means that the class derives from<br />

IDisposable .<br />

class SomeClass: IDisposable<br />

{<br />

// This class MUST contain an implementation of the<br />

// IDisposable.Dispose() method, otherwise<br />

// you get a compilation error.<br />

public void Dispose()<br />

{<br />

// implementation of Dispose() method<br />

}<br />

// rest of class<br />

}<br />

In this example, if SomeClass derives from IDisposable but doesn ’ t contain a Dispose() implementation<br />

with the exact same signature as defi ned in IDisposable , you get a compilation error because the class<br />

would be breaking its agreed - on contract to implement IDisposable . Of course, there ’ s no problem for<br />

the compiler about a class having a Dispose() method but not deriving from IDisposable . The problem,<br />

then, would be that other code would have no way of recognizing that SomeClass has agreed to support the<br />

IDisposable features.<br />

IDisposable is a relatively simple interface because it defi nes only one method. Most<br />

interfaces will contain more members.<br />

defining <strong>and</strong> implementing interfaces<br />

This section illustrates how to defi ne <strong>and</strong> use interfaces through developing a short program that follows the<br />

interface inheritance paradigm. The example is based on bank accounts. Assume that you are writing code<br />

that will ultimately allow computerized transfers between bank accounts. And assume for this example that<br />

there are many companies that may implement bank accounts, but they have all mutually agreed that any<br />

classes that represent bank accounts will implement an interface, IBankAccount , which exposes methods to<br />

deposit or withdraw money, <strong>and</strong> a property to return the balance. It is this interface that will allow outside<br />

code to recognize the various bank account classes implemented by different bank accounts. Although<br />

the aim is to allow the bank accounts to talk to each other to allow transfers of funds between accounts,<br />

we won ’ t introduce that feature just yet.<br />

To keep things simple, you will keep all the code for the example in the same source fi le. Of course, if<br />

something like the example were used in real life, you could surmise that the different bank account classes<br />

would not only be compiled to different assemblies, but would also be hosted on different machines owned<br />

by the different banks. That ’ s all much too complicated for our purposes here. However, to maintain some<br />

attempt at realism, you will defi ne different namespaces for the different companies.<br />

To begin, you need to defi ne the IBankAccount interface:<br />

namespace Wrox.ProCSharp<br />

{<br />

public interface IBankAccount<br />

{<br />

void PayIn(decimal amount);<br />

bool Withdraw(decimal amount);<br />

decimal Balance<br />

{<br />

get;<br />

}<br />

}<br />

}<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!