Information Technology Reference
In-Depth Information
Implementing Multiple Interfaces
In the examples shown so far, the classes have implemented a single interface.
￿
A class or struct can implement any number of interfaces.
￿
All the interfaces implemented must be listed in the base class list and separated by
commas (following the base class name, if there is one).
For example, the following code shows class MyData , which implements two interfaces:
IDataRetrieve and IDataStore . Figure 17-4 illustrates the implementation of the multiple
interfaces in the class shown in the preceding code.
interface IDataRetrieve { int GetData(); } // Declare interface
interface IDataStore { void SetData( int x ); } // Declare interface
Interface Interface
class MyData: IDataRetrieve, IDataStore // Declare class
{
int mem1; // Declare field
public int GetData() { return mem1; }
public void SetData( int x ){ mem1 = x; }
}
class Program {
static void Main() { // Main
MyData data = new MyData();
data.SetData( 5 );
Console.WriteLine("Value = {0}", data.GetData());
}
}
Figure 17-4. Class implementing multiple interfaces
Search WWH ::




Custom Search