using System;
// Step 1: Base interface
interface IShape
{
void Draw();
}
// Step 2: Derived interface inherits from IShape
interface IColorShape : IShape
{
void FillColor();
}
// Step 3: Implement the derived interface explicitly
class Rectangle : IColorShape
{
// Explicit implementation of Draw() from IShape
void IShape.Draw()
{
Console.WriteLine("Drawing a Rectangle");
}
// Explicit implementation of FillColor() from IColorShape
void IColorShape.FillColor()
{
Console.WriteLine("Filling Rectangle with Blue color");
}
}
class Program
{
static void Main()
{
// Create object of Rectangle
Rectangle rect = new Rectangle();
// To access explicit interface methods, we need to cast to interface
IShape shape = rect;
shape.Draw(); // Calls IShape.Draw()
IColorShape colorShape = rect;
colorShape.FillColor(); // Calls IColorShape.FillColor()
Console.ReadLine();
}
}
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter