What is encapsulation in C#?

Answer

Encapsulation is the OOP principle of bundling data (fields) with the methods that operate on that data, and restricting direct access to internal state. In C#, achieved through: (1) Access modifiers: making fields private and exposing them via public properties or methods. (2) Properties: C# properties provide controlled access to private fields with get and set accessors, where validation or computation can be added. Example: private int _age; public int Age { get => _age; set { if (value < 0) throw new ArgumentException("Age must be non-negative"); _age = value; } }. Benefits: data integrity (validate on set), ability to change internal implementation without affecting callers, and clear separation of what is public API vs internal implementation. Auto-properties: public string Name { get; set; } (compiler generates the backing field).