Structs: value semantics & immutability pattern
Understand struct value semantics in C# 6 and build an immutable struct pattern (copy-on-change).
Structs: value semantics & immutability pattern is a free C# Academy lesson on CoddyKit — lesson 1 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C# Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Structs overview
Goal: Use structs safely.
- Value semantics (copy on assign)
- Immutable-struct pattern in C# 6
- Passing by value vs by ref
- Boxing note
Copy semantics
Structs are value types: assignment makes a copy. Changing the copy does not change the original.
using System;
// Simple struct with public fields for demo (okay for small POD data)
public struct Point
{
public int X;
public int Y;
}
public class Program
{
public static void Main(string[] args)
{
Point a = new Point();
a.X = 1; a.Y = 2;
Point b = a; // copy the value
b.X = 9; // change the copy
Console.WriteLine("a=(" + a.X + "," + a.Y + ")");
Console.WriteLine("b=(" + b.X + "," + b.Y + ")"); // different from a
}
}
Immutable struct pattern
Prefer immutable structs: readonly fields, constructor for required state, and small With helpers for new values.
using System;
// Immutable struct pattern (C# 6): readonly fields + constructor + With method
public struct Money
{
private readonly decimal _amount;
private readonly string _currency;
public decimal Amount { get { return _amount; } }
public string Currency { get { return _currency; } }
public Money(decimal amount, string currency)
{
_amount = amount;
_currency = currency ?? "USD";
}
// Copy-on-change: returns a new value with updated amount
public Money WithAmount(decimal amount)
{
return new Money(amount, _currency);
}
}
public class Program
{
public static void Main(string[] args)
{
Money m1 = new Money(10m, "USD");
Money m2 = m1.WithAmount(15m); // new value
Console.WriteLine("m1: " + m1.Amount + " " + m1.Currency);
Console.WriteLine("m2: " + m2.Amount + " " + m2.Currency);
}
}
By value vs by ref
Methods receive a copy by default. Use ref to modify the caller's struct.
using System;
public struct Counter
{
public int Value;
}
public class Program
{
static void IncrementByValue(Counter c)
{
c.Value = c.Value + 1; // changes only the local copy
}
static void IncrementByRef(ref Counter c)
{
c.Value = c.Value + 1; // changes the caller's variable
}
public static void Main(string[] args)
{
Counter a = new Counter();
IncrementByValue(a);
Console.WriteLine("After by-value: " + a.Value); // 0
IncrementByRef(ref a);
Console.WriteLine("After by-ref: " + a.Value); // 1
}
}
Boxing basics
Boxing copies a struct into an object/interface reference. Unboxing creates another copy.
using System;
public interface IShow { string Show(); }
public struct Temperature : IShow
{
public int C;
public string Show() { return C + "C"; }
}
public class Program
{
public static void Main(string[] args)
{
Temperature t = new Temperature();
t.C = 25;
object boxed = t; // boxing: copy into object
Temperature copy = (Temperature)boxed; // unbox (another copy)
copy.C = 30;
Console.WriteLine("t = " + t.C); // 25
Console.WriteLine("copy = " + copy.C); // 30
}
}
Struct tips
Tips:
- Use structs for small, immutable value objects (coordinates, money).
- Avoid large or highly mutable structs.
- Remember copies on assign and when passing by value.
- Be mindful of boxing when casting to object/interface.
Struct value semantics
Recap
Recap: Structs are value types. Prefer small, immutable designs (constructor + readonly fields + With). Know how by-value vs ref passing and boxing affect copies.
Frequently asked questions
Is the “Structs: value semantics & immutability pattern” lesson free?
Yes — the full text of “Structs: value semantics & immutability pattern” is free to read here on the web, and the C# Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C# Academy course, upgrade to CoddyKit PRO.
What will I learn in “Structs: value semantics & immutability pattern”?
Understand struct value semantics in C# 6 and build an immutable struct pattern (copy-on-change). You practise C# Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C# Academy?
No prior experience is required. C# Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 3, so you can start here or from the beginning and move at your own pace.
How long does the “Structs: value semantics & immutability pattern” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C# Academy lesson?
Yes. Every C# Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Structs: value semantics & immutability pattern
- Record-like types & value-based equality (pattern)
- Enums & [Flags]