logo
CSharp_Prog_Guide

Надежное программирование

System.Console.WriteLine("Length: {0}", dimensions.getLength());

System.Console.WriteLine("Width: {0}", dimensions.getWidth());

How to: Explicitly Implement Interface Members with Inheritance

Explicit interface implementation also allows the programmer to implement two interfaces that have the same member names and give each interface member a separate implementation. This example displays the dimensions of a box in both metric and English units. The Boxclass implements two interfaces IEnglishDimensions and IMetricDimensions, which represent the different measurement systems. Both interfaces have identical member names, Length and Width.

Example

// Declare the English units interface:

interface IEnglishDimensions

{

float Length();

float Width();

}

// Declare the metric units interface:

interface IMetricDimensions

{

float Length();

float Width();

}

// Declare the Box class that implements the two interfaces:

// IEnglishDimensions and IMetricDimensions:

class Box : IEnglishDimensions, IMetricDimensions

{

float lengthInches;

float widthInches;

public Box(float length, float width)

{

lengthInches = length;

widthInches = width;

}