c-sharp
  1. c-sharp-encapsulation

C# Encapsulation

Encapsulation is an object-oriented programming concept that refers to the ability of an object to hide its data and methods from the outside world. In C#, encapsulation is achieved using access modifiers. In this tutorial, we'll discuss how to use encapsulation in C#.

Syntax

In C#, there are four access modifiers that can be used to control access to class members:

  • public: accessible from any code in the application
  • private: accessible only from within the class
  • protected: accessible from within the class and any derived classes
  • internal: accessible only within the same assembly (i.e., the same .exe or .dll file)

Here is an example of a class that uses encapsulation to hide its data from outside code:

class MyClass {
   private int myPrivateInt = 0;
   public void SetInt(int value) {
      myPrivateInt = value;
   }
   public int GetInt() {
      return myPrivateInt;
   }
}

In the example above, we've used the private access modifier to hide the "myPrivateInt" field from outside code. We've also created two public methods to set and get the value of the field.

Example

Let's create an example to demonstrate encapsulation:

class BankAccount {
   private decimal balance;

   public void Deposit(decimal amount) {
      balance += amount;
   }

   public void Withdraw(decimal amount) {
      balance -= amount;
   }

   public decimal GetBalance() {
      return balance;
   }
}

class Program {
   static void Main(string[] args) {
      BankAccount account = new BankAccount();
      account.Deposit(100.00M);
      account.Withdraw(50.00M);
      Console.WriteLine("Balance: {0:C}", account.GetBalance());
   }
}

Output

When we run the example code above, the output will be:

Balance: $50.00

This is because we deposited $100.00 into the account, then withdrew $50.00, leaving a balance of $50.00.

Explanation

In the example above, we created a BankAccount class that uses encapsulation to hide the balance field from outside code. We also provided public methods to deposit and withdraw funds, as well as to get the account balance.

In Main method, we created an instance of BankAccount, deposited $100.00, withdrew $50.00, and printed the account balance, which was $50.00.

Use

Encapsulation is an important concept in object-oriented programming as it helps ensure the integrity of your code. By hiding implementation details from outside code, you can provide a cleaner interface to your classes and prevent unintended access to class members.

Important Points

  • Access modifiers are used in C# to control access to class members to achieve encapsulation.
  • Access modifiers include public, private, protected, and internal.
  • Encapsulation helps ensure the integrity of your code and prevents unintended access to class members.

Summary

In this tutorial, we discussed how to use encapsulation in C#. We covered the syntax, example, output, explanation, use, and important points of encapsulation in C#. With this knowledge, you can now use access modifiers to control access to class members in your C# code and achieve encapsulation.

Published on: