public double Perimeter() {
return 4 * (this.length);
}
public double Area() {
return (Math.Pow(this.length, 2));
}
}
Here, you provide the implementation for the length property as well as the two methods — Perimeter()
and Area()
.
You not need to implement the IShape
class because you can't provide any meaningful implementation of the Area()
and Perimeter()
methods here.
Because the IRectangle
interface inherits from both the ISquare
and IShape
interfaces and the ISquare
interface has already been implemented (by the Square
class), you can simply inherit from the Square
class and implement the IRectangle
interface, like this:
public class Rectangle : Square, IRectangle {
//---property---
public double width { get; set; }
}
If you implement the IRectangle
interface directly (without inheriting from the Square
class, you need to provide the implementation of the length property as well as the methods Perimeter()
and Area()
.
You need only provide the implementation for the width property here. The implementation for the Area()
and Perimeter()
methods is inherited from the Square
class.
The last implementation is the ICircle
interface, for which you will implement the radius
property as well as the Perimeter()
and Area()
methods:
public class Circle : ICircle {
public double radius { get; set; }
public double Perimeter() {
return (2 * Math.PI * (this.radius));
}
//---provide the implementation for the virtual method---
public double Area() {
return (Math.PI * Math.Pow(this.radius, 2));
}
}
Figure 6-10 shows the classes that you have implemented for these three interfaces.

Figure 6-10
Explicit Interface Members Implementation
A class can implement one or more interfaces. To implement a member in an interface, you simply need to match the member name and its signature with the one defined in the interface. However, there are times when two interfaces may have the same member name and signature. Here's an example:
public interface IFileLogging {
void LogError(string str);
}
public interface IConsoleLogging {
void LogError(string str);
}
In this example, both IFileLogging
and IConsoleLogging
have the same LogError()
method. Suppose that you have a class named Calculation
that implements both interfaces:
public class Calculation : IFileLogging, IConsoleLogging {}
The implementation of the LogError()
method may look like this:
public class Calculation : IFileLogging, IConsoleLogging {
}
In this case, the LogError()
method implementation will be common to both interfaces and you can invoke it via an instance of the Calculation
class:
Calculation c = new Calculation();
//---prints out: Some error message here---
c.LogError('Some error message here');
In some cases, you need to differentiate between the two methods in the two interfaces. For example, the LogError()
method in the IFileLogging
interface may write the error message into a text file, while the LogError()
method in the IConsoleLogging
interface may write the error message into the console window. In that case, you must explicitly implement the LogError ()
method in each of the two interfaces. Here's how:
public class Calculation : IFileLogging, IConsoleLogging {
//---common to both interfaces---
public void LogError(string str) {
Console.WriteLine(str);
}
}
This example has three implementations of the LogError()
method:
□ One common to both interfaces that can be accessed via an instance of the Calculation
class.
□ One specific to the IFileLogging
interface that can be accessed only via an instance of the IFileLogging
interface.