Methods can change private state while the class controls what is allowed.

encapsulation Encapsulation keeps state changes inside the object that owns the state.

Encapsulated State

spend
EncapsulatedState.cs
Replay: real traced execution (multi-file project)
using System;

class Wallet
{
    private int cents;

    public int Cents
    {
        get { return cents; }
    }

    public void Deposit(int amount)
    {
        if (amount > 0)
        {
            cents += amount;
        }
    }

    public bool Spend(int amount)
    {
        if (amount <= cents)
        {
            cents -= amount;
            return true;
        }

        return false;
    }
}

class Program
{
    static void Main()
    {
        int spend = 30;
        Wallet wallet = new Wallet();
        wallet.Deposit(50);
        bool paid = wallet.Spend(spend);

        Console.WriteLine($"spend={spend}");
        Console.WriteLine($"paid={paid}");
        Console.WriteLine($"remaining={wallet.Cents}");
    }
}
using System;

class Wallet
{
    private int cents;

    public int Cents
    {
        get { return cents; }
    }

    public void Deposit(int amount)
    {
        if (amount > 0)
        {
            cents += amount;
        }
    }

    public bool Spend(int amount)
    {
        if (amount <= cents)
        {
            cents -= amount;
            return true;
        }

        return false;
    }
}

class Program
{
    static void Main()
    {
        int spend = 20;
        Wallet wallet = new Wallet();
        wallet.Deposit(50);
        bool paid = wallet.Spend(spend);

        Console.WriteLine($"spend={spend}");
        Console.WriteLine($"paid={paid}");
        Console.WriteLine($"remaining={wallet.Cents}");
    }
}
using System;

class Wallet
{
    private int cents;

    public int Cents
    {
        get { return cents; }
    }

    public void Deposit(int amount)
    {
        if (amount > 0)
        {
            cents += amount;
        }
    }

    public bool Spend(int amount)
    {
        if (amount <= cents)
        {
            cents -= amount;
            return true;
        }

        return false;
    }
}

class Program
{
    static void Main()
    {
        int spend = 80;
        Wallet wallet = new Wallet();
        wallet.Deposit(50);
        bool paid = wallet.Spend(spend);

        Console.WriteLine($"spend={spend}");
        Console.WriteLine($"paid={paid}");
        Console.WriteLine($"remaining={wallet.Cents}");
    }
}
  1. spend ← 30, wallet ← Wallet

    33{34    static void Main()35    {36        int spend→ 30 = 30; //@spend=20, 8037        Wallet wallet→ Wallet = new Wallet();38        walletWallet.Deposit(50);39        bool paid = wallet.Spend(spend);
  2. public void Deposit(int amount)

    12public void Deposit(int amount50)13{14    if (amount > 0)
  3. cents ← 50

    13{14    if (amount50 > 0)15    {16        cents→ 50 += amount50;17    }
  4. wallet.Deposit(50);

    37Wallet wallet = new Wallet();38walletWallet.Deposit(50);39bool paid = walletWallet.Spend(spend30);
  5. public bool Spend(int amount)

    20public bool Spend(int amount30)21{22    if (amount <= cents)
  6. cents ← 20

    21{22    if (amount30 <= cents50)23    {24        cents→ 20 -= amount30;25        return true;26    }
  7. paid ← True

    38wallet.Deposit(50);39bool paid→ True = walletWallet.Spend(spend30);4041Console.WriteLine($"spend={spend30}");42Console.WriteLine($"paid={paidTrue}");43Console.WriteLine($"remaining={wallet.Cents}");
    outputspend=30
    paid=True
  8. get

    pass 1 of 5
    8{9    get { return cents20; }10}
  9. Console.WriteLine($"remaining={wallet.Cents}");

    42    Console.WriteLine($"paid={paid}");43    Console.WriteLine($"remaining={wallet.Cents20}");44}
  10. Console.WriteLine($"remaining={wallet.Cents}");

    42    Console.WriteLine($"paid={paid}");43    Console.WriteLine($"remaining={wallet.Cents20}");44}
    outputremaining=20
  1. spend ← 20, wallet ← Wallet

    33{34    static void Main()35    {36        int spend→ 20 = 20;37        Wallet wallet→ Wallet = new Wallet();38        walletWallet.Deposit(50);39        bool paid = wallet.Spend(spend);
  2. public void Deposit(int amount)

    12public void Deposit(int amount50)13{14    if (amount > 0)
  3. cents ← 50

    13{14    if (amount50 > 0)15    {16        cents→ 50 += amount50;17    }
  4. wallet.Deposit(50);

    37Wallet wallet = new Wallet();38walletWallet.Deposit(50);39bool paid = walletWallet.Spend(spend20);
  5. public bool Spend(int amount)

    20public bool Spend(int amount20)21{22    if (amount <= cents)
  6. cents ← 30

    21{22    if (amount20 <= cents50)23    {24        cents→ 30 -= amount20;25        return true;26    }
  7. paid ← True

    38wallet.Deposit(50);39bool paid→ True = walletWallet.Spend(spend20);4041Console.WriteLine($"spend={spend20}");42Console.WriteLine($"paid={paidTrue}");43Console.WriteLine($"remaining={wallet.Cents}");
    outputspend=20
    paid=True
  8. get

    pass 1 of 5
    8{9    get { return cents30; }10}
  9. Console.WriteLine($"remaining={wallet.Cents}");

    42    Console.WriteLine($"paid={paid}");43    Console.WriteLine($"remaining={wallet.Cents30}");44}
  10. Console.WriteLine($"remaining={wallet.Cents}");

    42    Console.WriteLine($"paid={paid}");43    Console.WriteLine($"remaining={wallet.Cents30}");44}
    outputremaining=30
  1. spend ← 80, wallet ← Wallet

    33{34    static void Main()35    {36        int spend→ 80 = 80;37        Wallet wallet→ Wallet = new Wallet();38        walletWallet.Deposit(50);39        bool paid = wallet.Spend(spend);
  2. public void Deposit(int amount)

    12public void Deposit(int amount50)13{14    if (amount > 0)
  3. cents ← 50

    13{14    if (amount50 > 0)15    {16        cents→ 50 += amount50;17    }
  4. wallet.Deposit(50);

    37Wallet wallet = new Wallet();38walletWallet.Deposit(50);39bool paid = walletWallet.Spend(spend80);
  5. public bool Spend(int amount)

    20public bool Spend(int amount80)21{22    if (amount <= cents)23    {24        cents -= amount;25        return true;26    }2728    return false;29}
  6. paid ← False

    38wallet.Deposit(50);39bool paid→ False = walletWallet.Spend(spend80);4041Console.WriteLine($"spend={spend80}");42Console.WriteLine($"paid={paidFalse}");43Console.WriteLine($"remaining={wallet.Cents}");
    outputspend=80
    paid=False
  7. get

    pass 1 of 5
    8{9    get { return cents50; }10}
  8. Console.WriteLine($"remaining={wallet.Cents}");

    42    Console.WriteLine($"paid={paid}");43    Console.WriteLine($"remaining={wallet.Cents50}");44}
  9. Console.WriteLine($"remaining={wallet.Cents}");

    42    Console.WriteLine($"paid={paid}");43    Console.WriteLine($"remaining={wallet.Cents50}");44}
    outputremaining=50