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

How can you prevent a class from overriding in C#?

Câu trả lời

To prevent a class from being overridden in C#, you can use the sealed keyword. Here's how you can apply it:

Sealing a Class

To prevent a class from being inherited, simply add the sealed keyword before the class keyword:

csharp Copy
public sealed class MyClass
{
    // Class members
}

By marking a class as sealed, you ensure that no other class can inherit from it. If another class attempts to inherit from a sealed class, the compiler will throw an error.

Sealing a Virtual Method

You can also seal a virtual method within a class to prevent it from being overridden in derived classes. To do this, add the sealed keyword before the override keyword when overriding the virtual method:

csharp Copy
public class BaseClass
{
    public virtual void MyMethod()
    {
        // Method implementation
    }...
middle

middle

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

middle

What is scope of a Internal member variable of a C# class?

expert

Implement the Where method in C#. Explain

junior

What is the difference between a class and a structure ?

Bình luận

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

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