When to Use Struct vs Class in C#: Key Differences and Guidance
struct in C# for small, simple data that represents a single value and does not require inheritance. Use a class when you need reference semantics, inheritance, or more complex behavior.Quick Comparison
Here is a quick side-by-side comparison of struct and class in C# to highlight their main differences.
| Factor | Struct | Class |
|---|---|---|
| Type | Value type | Reference type |
| Memory allocation | Stack (usually) | Heap |
| Inheritance | No inheritance (except from interfaces) | Supports inheritance |
| Default constructor | Cannot define parameterless constructor (before C# 10) | Can define parameterless constructor |
| Immutability | Often used for immutable data | Mutable by default |
| Performance | Better for small data, less GC pressure | Better for complex objects and polymorphism |
Key Differences
Structs are value types, which means they hold their data directly and are copied when assigned or passed around. This makes them efficient for small data like points, colors, or simple records. They do not support inheritance except for implementing interfaces, and they cannot have a parameterless constructor defined by the user (prior to C# 10).
Classes are reference types, so variables hold references to the actual data on the heap. This allows for inheritance, polymorphism, and more complex behaviors. Classes can have parameterless constructors and support mutable state more naturally. Because they live on the heap, they involve garbage collection, which can affect performance for many small objects.
Choosing between them depends on whether you want value semantics (copy on assignment) or reference semantics (shared object), and on the size and complexity of the data you are modeling.
Code Comparison
Here is an example of a struct representing a 2D point with simple data and value semantics.
public struct PointStruct { public int X { get; } public int Y { get; } public PointStruct(int x, int y) { X = x; Y = y; } public override string ToString() => $"({X}, {Y})"; } class Program { static void Main() { PointStruct p1 = new PointStruct(3, 4); PointStruct p2 = p1; // Copies the data p2 = new PointStruct(5, 6); System.Console.WriteLine(p1); // Outputs: (3, 4) System.Console.WriteLine(p2); // Outputs: (5, 6) } }
Class Equivalent
Here is the same example using a class to show reference semantics.
public class PointClass { public int X { get; } public int Y { get; } public PointClass(int x, int y) { X = x; Y = y; } public override string ToString() => $"({X}, {Y})"; } class Program { static void Main() { PointClass p1 = new PointClass(3, 4); PointClass p2 = p1; // Copies the reference p2 = new PointClass(5, 6); System.Console.WriteLine(p1); // Outputs: (3, 4) System.Console.WriteLine(p2); // Outputs: (5, 6) } }
When to Use Which
Choose struct when:
- Your data is small and simple, like a coordinate or color.
- You want value semantics so copies are independent.
- You want to avoid heap allocation and reduce garbage collection.
- You do not need inheritance or complex behavior.
Choose class when:
- Your data is large or complex.
- You need inheritance or polymorphism.
- You want reference semantics so multiple variables share the same object.
- You need to define parameterless constructors or mutable state.
In summary, use struct for lightweight, immutable data and class for more complex, mutable objects.
Key Takeaways
struct for small, simple data with value semantics and no inheritance.class for complex data needing reference semantics and inheritance.