Hardware FixRecommendedDevice not working? Your driver may be the problemCheck updates for common hardware issues.Fix DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PC×
Skip to content
World desk4 min

Are Structs Always on the Stack in C#? How Memory Really Works

C# value types define copying behavior, not a universal stack location. See how inline storage, boxing, and ref struct restrictions affect memory and design choices.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Are structs always allocated on the stack in C#? No. A struct is a value type, which determines how values are copied and used; it does not guarantee that every instance lives on a thread’s stack. Struct data can be stored inline inside a heap-allocated object or array, and boxing creates a separate heap object. The explicit stack-bound category is ref struct, which has rules that prevent its values from escaping into unsafe contexts.

What does “value type” mean in C#?

For an ordinary struct, value-type semantics mean that a variable holds a value and assignment copies that value. For a class, assignment copies a reference to an object instead. These are language-level behaviors, not a promise about a particular memory address or region.

Point p = new Point(3, 4);
Point q = p;
q.X = 10;

After the assignment, q has its own copy of the value. Changing q.X does not change p.X. That remains true whether the runtime keeps a local value in a stack frame, a register, or another location as an implementation detail.

Microsoft’s C# structs guidance and the C# type-system documentation describe the value/reference distinction. The key point is to separate the variable’s semantics from where the runtime stores a particular instance.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Where can a struct’s data live?

A struct can be stored inline in whatever contains it. If a class has a struct field, the field’s bytes are part of that class object. If an array has struct elements, the values are stored inline in the array’s allocation; they are not separate heap objects for each element.

struct Point { public int X; public int Y; }

class Marker
{
    public Point Position;
}

Point[] points = new Point[100];

Here, the Marker object and the Point[] array are heap allocations under normal managed execution. Their contained struct data is part of those allocations. By contrast, an array of class references stores references in the array; each non-null class instance is a separate object allocation.

This inline arrangement can reduce indirection and help locality, but it does not make every struct universally faster. Large values can be costly to copy, and real performance depends on the code and runtime. Microsoft’s class-versus-struct design guidelines discuss inline storage and the trade-offs.

What happens when a struct is boxed?

Boxing happens when a value type must be represented as an object or an interface value in a way that requires a reference to an object. The runtime creates a managed-heap object to hold a copy of the struct.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Point point = new Point(3, 4);
object boxed = point;

boxed refers to the boxed copy. It is distinct from the original point; changing one does not mutate the other. Microsoft explains this operation in its boxing and unboxing documentation.

Do not assume that every call through an interface necessarily boxes a struct. Generic constrained calls and compiler or runtime optimizations can avoid boxing in some cases. Inspect the specific code path or profile it rather than relying on a blanket rule.

How is ref struct different?

ref struct is a deliberately restricted type category used for stack-bound APIs such as Span<T>. Its restrictions prevent values from escaping contexts where references they contain could become unsafe. Ordinary structs do not have these same escape restrictions.

Rules include restrictions on boxing, array elements, ordinary class fields, and capturing a ref struct in a lambda. C# 13 broadened some uses in async methods and iterators, but a ref struct still cannot be used across the relevant await or yield suspension point. Confirm the project’s language version and the applicable rules in Microsoft’s ref struct reference before relying on a particular pattern.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Should you choose a struct or a class?

Choose based on the type’s meaning and behavior, not a slogan about the stack. A struct is often a good fit for small, value-like data that has no need for object identity, shared mutable state, or class inheritance. Microsoft Learn gives “roughly 16 bytes or less” as a rule of thumb for struct size, not a language limit or universal performance threshold. Prefer immutable value types where practical.

A class is usually the clearer choice when identity or shared state matters: assigning a class variable shares a reference to the same object, while assigning a struct makes a copy. Structs also cannot inherit from arbitrary classes, although they can implement interfaces.

Consideration Struct Class
Assignment Copies the value Copies a reference; variables can refer to the same object
Storage within arrays Elements stored inline in the array allocation Array stores references; objects are separately allocated
Identity and shared mutation Usually represents a value rather than shared identity Supports shared object identity and state
Inheritance Cannot derive from arbitrary classes Can participate in class inheritance
Boxing May require a heap object when converted to object or an interface representation Already represented by an object reference

Consider the size and frequency of copies, whether values are boxed, how arrays are used, and what profiling says about the actual workload. Microsoft Learn cautions that “In most cases, there’s no significant difference in the performance cost of allocating a class instance on the heap versus allocating a struct instance on the stack.” See Objects – create instances of types. That guidance is a reason not to select a type solely to avoid heap allocation.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.