Can Multiple Inheritan...
Can Multiple Inheritan...
No, C# does not support multiple inheritance of classes. In C#, a class can only inherit from a single base class. However, C# provides an alternative mechanism called interfaces, which allows a class to implement multiple interfaces.
Interfaces in C# define a contract that a class must follow. They specify a set of methods, properties, events, and indexers that a class must implement. A class can implement multiple interfaces, allowing it to inherit behavior from multiple sources.
Here's an example:
public interface IFly
{
void Fly();
}
public interface ISwim
{
void Swim();
}
public class Duck : IFly, ISwim
{
public void Fly()
{
Console.WriteLine("The duck is flying.");
}
public void Swim()
{
Console.WriteLine("The duck is swimming.");
}
}
In this example, the Duck
class implements both the IFly
and ISwim
interfaces, allowing it to fly and swim.
senior
Gợi ý câu hỏi phỏng vấn
Chưa có bình luận nào