What is a 'struct' in C#?

A foundational guide to the 'struct' keyword in C#. Learn how structs are used to create lightweight value types and understand the key differences between a struct and a class.

In C#, when you want to create your own custom type, the default choice is usually a class. However, C# provides another, more lightweight option for creating types: the struct.

A struct is a value type that is typically used to encapsulate a small group of related variables, such as the coordinates of a point or the properties of a rectangle.

struct vs. class: The Key Difference

The most fundamental difference between a struct and a class is that a struct is a value type, while a class is a reference type.

This has major implications for how they are stored in memory and how they behave.

  • Value Types (struct): A variable of a struct type directly contains the data for the struct. When you copy a struct, you are creating a complete, independent copy of the data. They are typically stored on the stack, which makes them very fast to allocate and deallocate.

  • Reference Types (class): A variable of a class type holds a reference (or a pointer) to an object in memory. The actual object lives on the heap. When you copy a class variable, you are only copying the reference, not the object itself. Both variables will point to the same object.

Let's see this in action:

// A simple struct
public struct Point
{
    public int X; 
    public int Y;
}

// A simple class
public class PointClass
{
    public int X; 
    public int Y;
}

// Behavior with a struct (Value Type)
Point p1 = new Point { X = 10, Y = 20 };
Point p2 = p1; // p2 is a full copy of p1
p2.X = 99;

Console.WriteLine(p1.X); // Output: 10

// Behavior with a class (Reference Type)
PointClass pc1 = new PointClass { X = 10, Y = 20 };
PointClass pc2 = pc1; // pc2 is a reference to the same object as pc1
pc2.X = 99;

Console.WriteLine(pc1.X); // Output: 99

When Should You Use a struct?

The .NET guidelines provide some clear advice on when to choose a struct over a class. You should consider defining a type as a struct instead of a class if it meets all of the following conditions:

  1. It logically represents a single value, similar to primitive types like int or double. A Point or a Color are good examples.

  2. It has an instance size under 16 bytes. Structs are designed to be small and lightweight.

  3. It is immutable. You should not design structs to have their data change after they are created. While you can create mutable structs, they can lead to confusing behavior.

  4. It will not have to be boxed frequently. Boxing is the process of converting a value type to a reference type, and it can have performance costs.

If you are in doubt, the default and safe choice is always to use a class. The vast majority of the custom types you create will be classes.

Other Differences

  • Inheritance: Structs can implement interfaces, but they cannot inherit from another class or struct (they implicitly inherit from System.ValueType). Classes can use implementation inheritance.
  • Constructors: A struct cannot declare a parameterless constructor. The compiler always provides one that initializes all fields to their default values.

Conclusion

Structs are a powerful feature in C# for creating lightweight, value-type objects. They are ideal for small, immutable data structures where value semantics and performance are important. While classes are the default choice for most custom types, understanding when and why to use a struct is a key part of writing efficient and correct C# code. For most application-level development, you will primarily use classes, but knowing about structs helps you understand the fundamental type system of .NET.