🔥 Top Encapsulation Questions with Answers
Q1. What is encapsulation in C#? Answer: Encapsulation means hiding internal data of a class and exposing controlled access through public methods or properties. It binds data and behavior together to ensure data integrity and security. ✅ Example: class Account { private double balance; public void Deposit ( double amount) { if (amount > 0 ) balance += amount; } public double GetBalance () { return balance; } } Here, the balance is hidden — you can’t directly modify it, only through methods. Q2. Why is encapsulation important? Answer: Because it: Prevents unauthorized access to data. Makes code more secure and maintainable . Allows validation logic before updating data. Supports modularity (class is independent). ✅ Example: public void SetAge ( int age) { if (age >= 18 ) this .age = age; else Console.WriteLine( "Underage not allowed!" ); }...