Private balance with public Deposit method.
Ready — edit the code above and click Run.
using System;
class Program
{
static void Main()
{
var w = new Wallet();
w.Deposit(50);
Console.WriteLine(w.GetBalance());
class Wallet {
private decimal _balance;
public void Deposit(decimal amt) => _balance += amt;
public decimal GetBalance() => _balance;
}
}
}
Try solving on your own first, then reveal the official answer.
Encapsulation hides fields; expose controlled methods.