Câu hỏi phỏng vấn C#
Câu hỏi

Can Multiple Inheritan...

Câu trả lời

Can Multiple Inheritance be implemented in C# ?

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#

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:

csharp Copy
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.

Advantages of using interfa...

senior

senior

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

senior

What interface should your data structure implement to make the Where method work?

junior

Can multiple catch blocks be executed?

senior

What is difference between late binding and early binding in C#?

Bình luận

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

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