Referential transparency means replacing a method call with its return value without changing program behavior. It indicates that the method's observable output is fully represented by its inputs and return value. section 6.3
A method loses referential transparency when it reads hidden inputs or produces hidden outputs. Current time, database queries, private mutable fields, and mutations such as adding to a collection can make an apparently simple method behave unlike a mathematical function. section 6.3
Use the principle to distinguish transparent functions from methods with side effects:
- CalculateDiscount(products) is transparent because its product input and decimal result are explicit. section 6.3
- Increment(4) can be replaced with 5; an Increment method that changes internal state cannot. section 6.3
- AddComment returns a Comment but also mutates a collection, creating a hidden output. section 6.3
public decimal CalculateDiscount(Product[] products)
{
decimal discount = products.Length * 0.01m;
return Math.Min(discount, 0.2m);
}
Inspect a transparent calculation whose input and output are explicit in the method signature. section 6.3








