Câu hỏi phỏng vấn .NET Core
Câu hỏi

Does .NET support M...

Câu trả lời

.NET, including .NET Core, does not support multiple inheritance for classes in the traditional sense, meaning a class cannot inherit from more than one class. This limitation is primarily due to the "Diamond Problem," where ambiguity arises if two parent classes have methods with the same signature, making it unclear which method should be inherited by the child class[2][7].

However, .NET provides a way to achieve similar functionality through interfaces. This approach is known as "multiple interface inheritance" or "interface-based multiple inheritance." In this method, a class can implement multiple interfaces, allowing it to inherit the behavior defined by those interfaces without the complications associated with multiple class inheritance[1][4][5].

Example of Multiple Interface Inheritance in C#

public interface IFirst
{
    void FirstMethod();
}

public interface ISecond
{
    void SecondMethod();
}

public class First : IFirst
{
    public void FirstMethod() 
    {
        Console.WriteLine("First");
    }
}

public class Second : ISecond
{
    public void SecondMethod() 
    {
        Console.WriteLine("Second");
    }
}

public class FirstAndSecond : IFirst, ISecond
{
    First first = new First();
    Second second = new Second();

    public void FirstMethod() 
    {
        first.FirstMethod();
    }

    public void SecondMethod() 
    {
        second.SecondMethod();
    }
}

In this example, the FirstAndSecond class implements both IFirst and ISecond interfaces, effectively allowing it to inherit m...

senior

senior

Gợi ý câu hỏi phỏng vấn

middle

What are some benefits of using Options Pattern in ASP.NET Core?

middle

What is difference between .NET Core and .NET Framework?

junior

What is the difference between decimal , float and double in .NET?

Bình luận

Chưa có bình luận nào

Chưa có bình luận nào