Encapsulation in C# Last Updated : 17 Sep, 2025 Comments Improve Suggest changes Like Article Like Report Encapsulation is one of the core principles of object-oriented programming (OOP). It refers to the practice of binding data (fields) and the methods that operate on that data into a single unit, while restricting direct access to some components. This ensures controlled interaction with an object’s internal state.EncapsulationKey ConceptsEncapsulation hides the internal representation of an object and exposes only necessary operations.Fields are often kept private while access is provided through public properties or methods.It improves data security, code maintainability and flexibility.Access modifiers (private, public, protected, internal) control visibility of members.Example: C# using System; class Account { private double balance; // hidden field public void Deposit(double amount) { if (amount > 0) balance += amount; } public void Withdraw(double amount) { if (amount <= balance) balance -= amount; } public double GetBalance() { return balance; } } class GFG { static void Main() { Account acc = new Account(); acc.Deposit(500); acc.Withdraw(200); Console.WriteLine("Balance: " + acc.GetBalance()); } } OutputBalance: 300 Explanation:balance is private and cannot be accessed directly from outside.The class provides controlled methods (Deposit, Withdraw, GetBalance) to interact with the field.Encapsulation Using PropertiesC# provides properties to simplify encapsulation, acting like smart getters and setters. C# using System; class Student { private string name; // private field public string Name { get { return name; } set { if (!string.IsNullOrEmpty(value)) name = value; } } } class Program { static void Main() { Student s = new Student(); s.Name = "Alex"; Console.WriteLine("Student Name: " + s.Name); } } OutputStudent Name: Alex Explanation:The field name is private.The Name property controls how values are set and retrieved.This improves flexibility compared to public fields.AdvantagesData Protection: Prevents unauthorized access to fields.Controlled Access: Exposes only required operations.Code Flexibility: Internal implementation can change without affecting external code.Maintainability: Reduces coupling between classes.DisadvantagesUsing getters and setters adds extra code compared to accessing fields directly.Accessing data through methods may be a bit slower than direct access.Since data is hidden, it can sometimes be difficult to quickly inspect or change values during debugging. Comment More info A ankita_saini Follow Improve Article Tags : C# CSharp-OOP Explore IntroductionC# Tutorial 4 min read Introduction to .NET Framework 6 min read C# .NET Framework (Basic Architecture and Component Stack) 6 min read C# Hello World 2 min read Common Language Runtime (CLR) in C# 4 min read FundamentalsC# Identifiers 2 min read Data Types in C# 6 min read C# Variables 4 min read C# Literals 5 min read Operators in C# 7 min read C# Keywords 5 min read Control StatementsC# Decision Making (if, if-else, if-else-if ladder, nested if, switch, nested switch) 5 min read C# Switch Statement 4 min read Loops in C# 4 min read C# Jump Statements (Break, Continue, Goto, Return and Throw) 4 min read OOP ConceptsClass and Objects in C# 4 min read Constructors in C# 5 min read C# Inheritance 3 min read C# Encapsulation 4 min read C# Abstraction 4 min read MethodsMethods in C# 4 min read Method Overloading in C# 4 min read C# | Method Parameters 7 min read Method Overriding in C# 7 min read Anonymous Method in C# 3 min read ArraysArrays in C# 6 min read Jagged Arrays in C# 4 min read Array Class in C# 5 min read How to Sort an Array in C# | Array.Sort() Method Set - 1 8 min read How to find the rank of an array in C# 2 min read ArrayListArrayList in C# 6 min read C# ArrayList Class 7 min read C# | Array vs ArrayList 2 min read StringStrings in C# 6 min read C# Verbatim String Literal - @ 5 min read C# String Class 9 min read C# StringBuilder 4 min read C# String vs StringBuilder 3 min read TupleC# Tuple 7 min read C# Tuple Class 3 min read C# ValueTuple 7 min read C# ValueTuple Struct 4 min read IndexersC# Indexers 4 min read C# Multidimensional Indexers 5 min read C# - Overloading of Indexers 3 min read Like