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

in C#, when should we use abstract classes i...

Câu trả lời

In C#, abstract classes and interfaces with extension methods serve different purposes and are used in different scenarios. Here are some guidelines on when to use each:

Abstract Classes

Use abstract classes when:

  1. You want to provide some default implementation for methods: Abstract classes can contain both abstract and non-abstract (concrete) methods. This allows you to provide some default behavior while still allowing derived classes to override specific methods.

  2. You need to share state between methods: Abstract classes can have fields and properties that can be accessed and shared by all derived classes.

  3. You want to restrict inheritance: You can mark an abstract class as sealed, preventing it from being inherited.

  4. You need to provide constructor parameters: Abstract classes can have constructors, which is useful when you need to pass parameters to initialize the state of the class.

  5. You want to provide static methods and constants: Abstract classes can have static methods and constants, which can be accessed without creating an instance of the class.

Interfaces with Extension Methods

Use interfaces with extension methods when:

  1. **You want to add new functionality to existing types w...
expert

expert

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

senior

Why to use lock statement in C#?

middle

Explain the difference between Task and Thread in .NET

senior

What is the difference between System.ApplicationException class and System.SystemException class?

Bình luận

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

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