Defining Interfaces
To declare an interface, you use the interface
keyword rather than class
or struct
. In the interface body, zero or more members can be specified, without any implementation provided. For example:
public interface IDisposable{ void Dispose();}
All this says is that a type implementing this interface promises to support the disposal operation by providing an implementation for the Dispose
method.
Note: Contracts or Not?
Where some people disagree with the idea of interfaces as contracts is that they do not really specify a “complete contract” beyond a series of signatures that are merely a syntactic concern. For example, what’s a type supporting IDisposable
supposed to do if Dispose
is called multiple times? Or what does ...
Get C# 5.0 Unleashed now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.