// Exercise 3: records, value equality, and `with` expressions // // Implement: // - record Money(string Currency, decimal Amount) with a method Add() // that returns a NEW Money using a `with` expression (records are // conceptually immutable — never mutate in place). // - A plain (non-record) class BankNote for contrast, to show reference // equality still applies to ordinary classes. using System; record Money(string Currency, decimal Amount) { public Money Add(decimal amount) { // TODO: return a new Money with Amount increased by `amount`, // using a `with` expression. Do not mutate `this`. throw new NotImplementedException(); } } class BankNote { public decimal Amount { get; init; } } class Program { static void Main() { int pass = 0, total = 0; var m1 = new Money("USD", 10m); var m2 = new Money("USD", 10m); total++; if (m1 == m2) { Console.WriteLine("PASS: records compare by value"); pass++; } else Console.WriteLine("FAIL: records compare by value"); total++; if (!ReferenceEquals(m1, m2)) { Console.WriteLine("PASS: records are still distinct heap objects"); pass++; } else Console.WriteLine("FAIL: records are still distinct heap objects"); var m3 = m1.Add(5m); total++; if (m3.Amount == 15m && m1.Amount == 10m) { Console.WriteLine("PASS: Add() returns new instance, original unchanged"); pass++; } else Console.WriteLine("FAIL: Add() returns new instance, original unchanged"); var n1 = new BankNote { Amount = 10m }; var n2 = new BankNote { Amount = 10m }; total++; if (n1 != n2) { Console.WriteLine("PASS: plain classes use reference equality by default"); pass++; } else Console.WriteLine("FAIL: plain classes use reference equality by default"); Console.WriteLine($"--- {pass}/{total} checks passed ---"); } }