Struct and class semantics
Struct and class semantics
A struct variable contains a value of its type; a class variable contains a reference to an object (or null). Ordinary assignment of a struct copies its fields. Ordinary assignment of a class reference copies the reference, so both variables can designate one object. These are semantic rules, not a universal stack-versus-heap rule: a struct field can be embedded inside a managed object and a value can be boxed.
A value type may itself contain a reference field. Copying that field copies the reference, not the referenced object. Prefer simple immutable structs for values such as coordinates; use classes for independently identified objects with shared lifetime. readonly struct restricts instance mutation but does not freeze objects referred to by its fields.
using System;var point = new Point { X = 3 };var pointCopy = point;pointCopy.X = 8;var box = new Box { X = 3 };var alias = box;alias.X = 8;Console.WriteLine(point.X);Console.WriteLine(box.X);struct Point { public int X; }class Box { public int X; }The Point copy has independent X storage; alias and box hold references to the same Box.
Expected output (reasoned, not executed):
38Build on the first example
using System;using System.Collections.Generic;var original = new Bundle(new List<string> { "pen" });var copy = original;copy.Names.Add("pad");Console.WriteLine(original.Names.Count);Console.WriteLine(object.ReferenceEquals(original.Names, copy.Names));readonly struct Bundle{ public List<string> Names { get; } public Bundle(List<string> names) => Names = names;}The readonly wrapper prevents reassignment of its own property through normal mutation; the list remains mutable and shared. This intentionally demonstrates a design risk.
Expected output (reasoned, not executed):
2TrueApply the idea to another case
using System;Console.WriteLine(new Counter().Value);Console.WriteLine(default(Counter).Value);struct Counter{ public int Value { get; } public Counter() => Value = 5;}In C# 14 the explicit public parameterless struct constructor runs for new Counter(). default(Counter) produces zero-initialized fields without invoking it. Arrays of Counter likewise start with default elements.
Expected output (reasoned, not executed):
50Common mistakes and design choices
Do not assume new is required for every struct value or that constructor validation excludes default values. Design an explicit policy for zero/default state. Passing a value by ordinary parameter copies it, while ref parameters can alias variable storage (review chapter 8). ref struct is a specialized ref-like value type with compiler-enforced escape restrictions; it cannot be boxed into object. Detailed spans and lifetime rules belong to a later chapter.
Practice
Create a readonly struct Distance with a get-only integer Meters property. Copy it, assign a new Distance(20) to the copy and print original and copy after starting at 5. Explain the default value.
Use the manual practice task and knowledge check to verify your understanding. All examples target C# 14 / .NET 10; execution and ByteMind renderer support remain unverified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Struct and class semantics — knowledge check
Apply the lesson concepts to the following scenarios.
What does ordinary struct assignment copy?
What does ordinary class reference assignment copy?
Can readonly struct contain a mutable list?
What is default(Counter).Value in example 3?
Can a ref struct be boxed into object?