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

How encapsulation is implemented in C#?

Câu trả lời

Encapsulation in C# is implemented by:

  1. Declaring class members (fields, properties, methods) with appropriate access modifiers to control their visibility and accessibility. The most commonly used access modifiers are:

    • private: accessible only within the same class
    • protected: accessible within the same class and derived classes
    • internal: accessible within the same assembly
    • public: accessible from anywhere[1][4]
  2. Providing public properties to access and manipulate private fields. Properties consist of a getter and a setter method that control how the field is accessed and modified. This allows you to enforce validation rules and maintain data integrity[1][3][5].

  3. Hiding implementation details from the outside world. By making fields private and exposing them through public properties, you can change the internal implementation without affecting the client code that uses the class[3][4].

Here's an example of how encapsulation is implemented using properties in C#:

public class BankAccount
{
    private decimal _balance;

    public decimal Balance
    {
        get { return _balance; ...
middle

middle

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

junior

What is enum in C#?

expert

in C#, when should we use abstract classes instead of interfaces with extension methods?

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