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:

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:

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

middle

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

entry

What is the difference between continue and break statements in C#?

junior

What are generics in C#?

senior

What's the difference between StackOverflowError and OutOfMemoryError ?

Bình luận

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

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