Information Technology Reference
In-Depth Information
Implementing an Interface
An interface can only be implemented by either a class or a struct. As shown in the Sort exam-
ple, to implement an interface, a class or struct must
￿
Include the name of the interface in its base class list.
￿
Supply implementations for each of the interface's members.
For example, the following code shows a new declaration for class MyClass , which imple-
ments interface IMyInterface1 , declared in the previous section. Notice that the interface
name is listed in the base class list after the colon, and that the class provides the actual
implementation code for the interface members.
Colon Interface name
class MyClass: IMyInterface1
{
int DoStuff ( int nVar1, long lVar2 )
{ ... } // Implementation code
double DoOtherStuff( string s, long x )
{ ... } // Implementation code
}
Some important things to know about implementing interfaces are the following:
￿
If a class implements an interface, it must implement all the members of that interface.
￿
If a class is derived from a base class and also implements interfaces, the name of the
base class must be listed in the base class list before any interfaces, as shown here:
Base class must be first Interface names
class Derived : MyBaseClass, IIfc1, IEnumerable, IEnumerator
{
...
}
Search WWH ::




Custom Search