Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

C# Struct Declaration: Syntax, Semantics, and Performance

Learn how to declare and use C# structs, understand value type copying semantics, and decide when a struct is a better choice than a class.

~4 min readC#
#struct#value types#C# syntax#memory allocation
Diagram showing a C# struct declaration with value type semantics and memory allocation.

In C#, you declare a struct with the struct keyword. A struct is a value type, so assignments and method arguments copy the data by value rather than sharing a reference. The sections below cover declaration syntax, value-type behavior, initialization rules, performance tradeoffs, and common pitfalls.

Declaring a Struct in C#

A struct can encapsulate data and related functionality. The basic declaration uses the struct keyword:

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

This declares a Point struct with two public integer fields. You can also define properties, methods, and constructors inside a struct. Structs are value types, so they are typically allocated on the stack or inline within containing objects; assignment copies the value rather than a reference.

The declaration syntax is similar to a class, but the semantics differ in fundamental ways. A struct cannot inherit from another struct or class, and it cannot be the base of a class. It implicitly inherits from System.ValueType, which in turn inherits from System.Object.

Value Type Semantics and Copying

When you assign a struct variable to another, the entire value is copied. This is different from references, where only the reference is copied.

csharp
Point a = new Point { X = 1, Y = 2 }; Point b = a; b.X = 10; Console.WriteLine(a.X); // Still 1

Because Point is a struct, b gets a full copy of a's data. Changing b.X does not affect a. This behavior is useful when you want to pass data without allowing the receiver to modify the original. However, it also means that large structs can cause performance overhead when copied frequently.

When to Use a Struct vs a Class

The decision between struct and class depends on the data size, usage pattern, and required semantics. Use a struct when:

  • The instance is small and short-lived.
  • The value is logically a single, indivisible unit.
  • You want value semantics, where equality is based on the data rather than identity.
  • You want to avoid extra heap allocations and garbage collection pressure.

A class is more appropriate when:

  • The object is large or contains many fields.
  • You need reference identity or shared references.
  • You need inheritance or polymorphism.

A common guideline is to use a struct for types under 16–24 bytes, but that is not a hard rule. Measure and profile when performance matters.

Struct Constructors and Initialization Rules

In C# versions before 10, you cannot declare an explicit parameterless constructor; the default constructor zero-initializes all fields. Starting with C# 10, you can declare one:

csharp
public struct Temperature { public double Celsius; public Temperature(double celsius) => Celsius = celsius; // C# 10 or later: public Temperature() => Celsius = 0; }

Every instance constructor must leave all fields in a definitely assigned state, either through explicit assignment or field initializers. Before C# 10, instance field initializers are not allowed in structs. From C# 10 onward, field initializers are allowed and run as part of the constructor.

readonly struct and ref struct

C# provides modifiers for specialized struct behavior. A readonly struct requires all instance fields to be readonly, which prevents accidental mutation and enables the compiler to avoid defensive copies when accessing members.

csharp
public readonly struct Distance { public readonly int Meters; public Distance(int meters) => Meters = meters; }

A ref struct is a stack-only struct that cannot be boxed or used in async methods. It is intended for high-performance scenarios such as Span<T>.

csharp
public ref struct MyRefStruct { public int Value; }

Ref structs cannot be captured by closures or used in async methods because they might escape the stack.

Performance Implications of Structs

Structs can reduce heap allocations and garbage collection overhead. When you create an array of structs, the memory is contiguous, improving cache locality. However, copying a large struct is more expensive than copying a reference. Also, boxing occurs when you convert a struct to an interface or object, which allocates on the heap.

csharp
Point p = new Point { X = 1, Y = 2 }; object boxed = p; // Boxing

Boxing is a performance hit and should be avoided in hot paths. Using generics with structs avoids boxing, because the runtime can specialize the code.

Common Mistakes and Pitfalls

One common mistake is mutating a struct through a property or method when it is stored in a read-only context. For example, if you have a readonly field of a struct type, calling a method that modifies its fields causes a compiler error because the compiler cannot guarantee the method does not mutate the struct.

Another issue is using a struct when reference semantics are needed. If you pass a struct to a method and expect the method to modify the original, you must use the ref or out modifier.

csharp
public void Modify(ref Point p) { p.X = 100; }

Without ref, the method receives a copy and the change is lost.

Structs in Collections and Async Code

When you store structs in collections like List<T>, the backing storage holds the struct values directly. The indexer returns a copy, so to modify an element, copy it out, change the copy, and write it back:

csharp
List<Point> points = new List<Point>(); points.Add(new Point { X = 1, Y = 2 }); Point temp = points[0]; temp.X = 5; points[0] = temp;

Async methods cannot have ref or out parameters, and a ref struct cannot be used in an async method. This limits the use of certain struct types in asynchronous code.

Try matching browser tools

All tools

Small utilities that fit C# work — they run entirely in this browser tab, so nothing you paste leaves your device.

All C# guides

More in C#

All C# guides

The guides filed next to this one in C#, in library order.