C# composition over inheritance, static classes, records and with expressions.
In one line
Composition keeps another object in a property instead of inheriting from it. static members belong to the class itself, not to objects. A record is a small type compared by value.
Why you need it
If an order inherited from an address, you would be saying "an order is an address", which is nonsense, and every new address field would ripple into orders. When an order holds an address as a property, each can change on its own.
Work that belongs to no object, like tax, and values with exactly one copy for the whole class, like the order count, fit static and are used without creating objects.
Everyday picture
Think of a lunch box. It has rice, side dishes and soup (composition); the rice is not the lunch box’s parent. The board on the shop wall showing "boxes sold today" exists once for the shop, not once per box (static). Two side dishes with the same name and price are simply the same dish (record).
Example code
Program.cs · 42 lines
var home = newAddress("12 Pine St", "Busan");var order = newOrder(home);order.Add(newItem("Pizza", 18_000m));order.Add(newItem("Cola", 2_000m));var office = home with { Street = "7 Harbor Rd" };var second = newOrder(office);second.Add(newItem("Pizza", 18_000m));Console.WriteLine($"#{order.Number} {order.Total():N0} won to {order.To.City}");Console.WriteLine($"#{second.Number} to {second.To.Street}, same city: {office.City == home.City}");Console.WriteLine($"Same first item: {order.First == second.First}, orders: {Order.Count}");recordAddress(stringStreet, stringCity);recordItem(stringName, decimalPrice);staticclassTax{publicconstdecimalRate = 0.1m;publicstaticdecimalAdd(decimal amount) => amount * (1 + Rate);}classOrder{privatestaticint count;privatereadonlyList<Item> items = [];publicintNumber { get; }publicAddressTo { get; }publicstaticintCount => count;publicItemFirst => items[0];publicOrder(Address to) { count++;Number = count;To = to; }publicvoidAdd(Item item) => items.Add(item);publicdecimalTotal() => Tax.Add(items.Sum(i => i.Price));}
C# code does not run on this page. Run your edited code on your own computer with dotnet run.
When to use it
"X has a Y" fits (an order has an address)
Calculations or constants with no object state (tax rate, unit conversion). Put them in a static class
Small data that counts as equal when the values match (address, point, amount). Use a record
When in doubt, pick composition over inheritance. A held object is easy to swap later.
A const is baked in at compile time. For settings that may change, use static readonly.
Common mistakes
Making a value-only type a class and comparing with ==
For a class, == asks whether both sides are the same object. Make it a record to compare values; the compiler writes Equals, GetHashCode and ToString for you.
Wrong
var a = newItem("Pizza", 18_000m);var b = newItem("Pizza", 18_000m);Console.WriteLine(a == b);classItem(string name, decimal price){publicstringName => name;publicdecimalPrice => price;}
When you run itFalse
Fixed
var a = newItem("Pizza", 18_000m);var b = newItem("Pizza", 18_000m);Console.WriteLine(a == b);recordItem(stringName, decimalPrice);
When you run itTrue
Creating a static class with new
A static class is a toolbox used without objects. You cannot create one; call it through the class name, as in Tax.Add(...).
Wrong
var t = newTax();
What the C# compiler sayserror CS0712: Cannot create an instance of the static class 'Tax'
Fixed
Console.WriteLine(Tax.Add(1_000m));
When you run it1100.0
Reading an object field from a static member
A static property runs without knowing which cart it belongs to, so it cannot read the per-object items. Members that use object values should not be static.
After changing only the city with with, what is printed?
var a = newAddress("1 Oak St", "Seoul");var b = a with { City = "Jeju" };Console.WriteLine($"{a.City} {b.City} {a == b}");
Show the answer
Answer: 2. Seoul Jeju Falsewith leaves the original record as it is and makes a new one. The cities differ, so == is False.
Question 2
After creating three orders, what is Order.Count?
var a = newAddress("x", "y");newOrder(a);newOrder(a);newOrder(a);Console.WriteLine(Order.Count);
Show the answer
Answer: 2. 3count is static, so every order shares one slot. Each creation adds 1, giving 3.
Questions people ask
How is a record different from a class?
A record compares property values with == and Equals, prints all properties in ToString, and supports with to copy it with a few changes. It suits small value types; objects with lots of behavior stay classes.
When should I write a static class?
To collect helpers and constants that need no object state, the way Math or File work. A static class cannot be instantiated or inherited.
How do I choose between const and static readonly?
const for numbers and strings that never change; static readonly for values decided at run time or that may change later. A const is copied into the calling code, so changing it means rebuilding the callers too.