एकाधिक वंशानुक्रम C# में समर्थित नहीं है। एकाधिक विरासतों को लागू करने के लिए, इंटरफेस का उपयोग करें।
यहाँ क्लास शेप में हमारा इंटरफ़ेस पेंटकॉस्ट है -
public interface PaintCost {
int getCost(int area);
} आकृति हमारा आधार वर्ग है जबकि आयत व्युत्पन्न वर्ग है -
class Rectangle : Shape, PaintCost {
public int getArea() {
return (width * height);
}
public int getCost(int area) {
return area * 80;
}
} आइए अब C# में कई इनहेरिटेंस के लिए इंटरफेस को लागू करने के लिए पूरा कोड देखें -
Using System;
namespace MyInheritance {
class Shape {
public void setWidth(int w) {
width = w;
}
public void setHeight(int h) {
height = h;
}
protected int width;
protected int height;
}
public interface PaintCost {
int getCost(int area);
}
class Rectangle : Shape, PaintCost {
public int getArea() {
return (width * height);
}
public int getCost(int area) {
return area * 80;
}
}
class RectangleDemo {
static void Main(string[] args) {
Rectangle Rect = new Rectangle();
int area;
Rect.setWidth(8);
Rect.setHeight(10);
area = Rect.getArea();
// Print the area of the object.
Console.WriteLine("Total area: {0}", Rect.getArea());
Console.WriteLine("Total paint cost: ${0}" , Rect.getCost(area));
Console.ReadKey();
}
}
}