Loading...

Structs in C# – Differences from Classes

Learn the key differences between structs and classes in C#, including memory model, inheritance, boxing, and performance.

In C#, class and struct are two fundamental types that bring data and behavior together. Although they may look similar at first glance, they differ significantly in memory management and intended use. class types behave as reference types, while struct types behave as value types.


What Is a Struct?

A struct is typically used to represent small, short-lived, and simple sets of data. It’s suitable for scenarios like a 2D coordinate point or a date range. By default, it is a value type—meaning when copied, a new independent copy is created.


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

    public Point(int x, int y)
    {
        X = x;
        Y = y;
    }

    public void Print()
    {
        Console.WriteLine($"({X}, {Y})");
    }
}

Differences Between Struct and Class

Below are the most fundamental differences between struct and class:


Copying Behavior

When structs are copied, a completely new copy is created. With classes, only the reference is passed, and two variables point to the same object.


Point p1 = new Point(10, 20);
Point p2 = p1; // copied
p2.X = 99;

Console.WriteLine(p1.X); // 10 (independent copy)
Console.WriteLine(p2.X); // 99

class Person
{
    public string Name { get; set; }
}

var p1 = new Person { Name = "John" };
var p2 = p1; // same reference
p2.Name = "Michael";

Console.WriteLine(p1.Name); // "Michael"
Console.WriteLine(p2.Name); // "Michael"

Reference Types vs. Value Types

In C#, types are broadly divided into value types and reference types. A struct is a value type; when assigned to a variable or passed to a method, all data is copied. So changes on one copy do not affect the other. A class is a reference type; when assigned to a variable, a reference pointing to the same object in memory is copied. Therefore, a change through one reference is visible through other references as well.


// Value type (struct)
Point p1 = new Point(1, 2);
Point p2 = p1;   // copy
p2.X = 99;
Console.WriteLine(p1.X); // 1
Console.WriteLine(p2.X); // 99

// Reference type (class)
Person a = new Person { Name = "Alice" };
Person b = a;    // same object
b.Name = "Mary";
Console.WriteLine(a.Name); // Mary
Console.WriteLine(b.Name); // Mary

When to Use Struct vs. Class?

Use a struct for small, immutable (or mostly immutable), and behaviorally limited data. Prefer a class for large, complex objects with longer lifecycles and that may require inheritance. For example, structs make sense for 3D points, color values, or date ranges; classes are more suitable for concepts like customers, products, and orders.


Example: Calculating with a Struct


public struct Point2D
{
    public int X { get; }
    public int Y { get; }

    public Point2D(int x, int y)
    {
        X = x;
        Y = y;
    }

    public double DistanceTo(Point2D other)
    {
        int dx = X - other.X;
        int dy = Y - other.Y;
        return Math.Sqrt(dx * dx + dy * dy);
    }
}

class Program
{
    static void Main()
    {
        var p1 = new Point2D(0, 0);
        var p2 = new Point2D(3, 4);

        Console.WriteLine(p1.DistanceTo(p2)); // 5
    }
}

TL;DR

  • struct = value type, class = reference type.
  • Use structs for small and simple data sets; use classes for complex objects.
  • Copying a struct creates an independent copy; copying a class copies a reference to the same object.
  • Structs cannot inherit but can implement interfaces; classes can inherit.
  • Structs are non-null by default; classes can be null.