C#: Access modifiers code errors and encapsulation.
Here is the example that was given in C# course on encapsulation. class BankAccount { private double balance=0; public void Deposit(double n) { balance += n; } public void Withdraw(double n) { balance -= n; } public double GetBalance() { return balance; } } class Program { static void Main(string[] args) { BankAccount b = new BankAccount(); b.Deposit(199); b.Withdraw(42); Console.WriteLine(b.GetBalance()); } I decided to play with it and changed it like this: class BankAccount { public double balance=0; public void Deposit(double n) { balance += n; } public void Withdraw(double n) { balance -= n; } public double GetBalance() { return balance; } } class Program { static void Main(string[] args) { BankAccount b = new BankAccount(); b.Deposit(199); b.Withdraw(42); balance.Deposit(100); Console.WriteLine(b.GetBalance()); } The error appeared, that balance does not exist in this context. Why? I thought changing the access modifier from private to public will make me able to access this variable.