Pure Functions Implementation in C#
14-day free trial
Try SeekerPro free for 14 days.
Full access while you try it · then $15.99/mo · cancel anytime
Start your 14-day free SeekerPro trial →30-day money-back guarantee · Cancel anytime
Not checking a site today? Turn this topic into a ready to paste AI prompt, free →
Key Takeaways
- ✓Pure Functions runs in O(1) time and O(1) space, in C# as in every other language.
- ✓record and record struct generate value equality and a with expression for non-destructive updates; init-only setters allow object-initialiser construction and nothing after; readonly struct promises the whole type is immutable.
- ✓Declare data as a record with init properties and update it with with { Total = ... }, which is the language-level copy-and-modify.
- ✓The C# trap: readonly on a field holding a MUTABLE struct forces the compiler to make a defensive copy on every member access, so calling a mutating method appears to succeed and then silently changes nothing, the write landed on the temporary.
- ✓with performs a shallow clone, so it is O(1) in fields and allocates once. readonly struct removes the defensive copies that a plain struct in a readonly field would otherwise pay for on every access, which is why the modifier is a performance feature as much as a correctness one.
What Pure Functions actually is
Pure Functions in C#: what you actually reach for
C# implementation
using System.Collections.Immutable;
record Line(string Sku, int Qty);
record Order(string Id, ImmutableArray<Line> Lines)
{
public Order Adding(Line line) => this with { Lines = Lines.Add(line) };
public int Total => Lines.Sum(l => l.Qty);
}
readonly struct Money // whole type promises no mutation
{
public Money(decimal amount) => Amount = amount;
public decimal Amount { get; init; }
public Money Plus(decimal n) => this with { Amount = Amount + n };
}
// record equality is shallow: these are NOT equal, the list references differ
record Shallow(List<int> Items);Think Your Code Is Clean? Let NexusBro QA It in 20 Seconds.
Paste your code. Click QA. Get an instant expert-level audit with fixes.
QA My Code FreeThe C#-specific trap
Performance in C#, beyond the O(1)
Real-World Applications
Interview Tips & Practice
Frequently Asked Questions
Which C# type or library should I use for Pure Functions?
record and record struct generate value equality and a with expression for non-destructive updates; init-only setters allow object-initialiser construction and nothing after; readonly struct promises the whole type is immutable. System.Collections.Immutable supplies ImmutableArray, ImmutableList and ImmutableDictionary, which return a new collection from every operation.
What is the most common C# mistake when implementing Pure Functions?
readonly on a field holding a MUTABLE struct forces the compiler to make a defensive copy on every member access, so calling a mutating method appears to succeed and then silently changes nothing, the write landed on the temporary. Separately, a record is shallow: a record holding a List<T> gives you value equality on the reference, so two records with equal-looking lists compare unequal, and either one can mutate the shared list.
How does Pure Functions actually perform in C#?
with performs a shallow clone, so it is O(1) in fields and allocates once. readonly struct removes the defensive copies that a plain struct in a readonly field would otherwise pay for on every access, which is why the modifier is a performance feature as much as a correctness one. ImmutableList is a balanced tree with O(log n) indexing; ImmutableArray is a flat array with O(1) reads and O(n) updates, and is usually the right one for read-mostly data. Time complexity is O(1) and space is O(1).
Related Articles
Rather try before you buy?
Get the rest of the vault: every cheat sheet, guide and audit in SeekerPro, free for 14 days, then $15.99/mo. Cancel anytime. Daily usage limits apply.
Start my 14-day SeekerPro trialStudying for this? Devin free tiers and academic plans are laid out on Noizz.
Is your site built to last?
Run a free QA audit and get your Site Health Score in seconds.
Check Your Site FreeNo signup required
Reader comments
No account needed. Share what worked, what did not, and what you would add.
Add your take below. A concrete tip, a result or a question helps the next reader most.