C# Struct vs Class: Key Differences 🔑
What's the difference between a struct and a class?
The main difference between structs and classes in C# is that structs are value types, while classes are reference types. Classes support inheritance, but structs don't. Classes can have a null reference, while structs can't.
Reference types are allocated on the heap and garbage-collected, whereas value types are allocated on the stack or inline and deallocated when the stack unwinds or the containing type is deallocated.
This table summarizes the differences between structs and classes:
|Reference type||Value type|
|Support inheritance||No inheritance|
|Can have null reference||Cannot have null reference|
|Contain complex logic||Light on logic, they structure data|
|Can have parameterless constructors||Can have parameterless constructors|
What is a struct?
A C# struct is a value type with the main purpose of storing data in a structured way. Classes are more about defining behavior, while structs give us a way to structure data.
Structs are stored on the stack and they cannot be null. Unlike classes, structs do not support inheritance. Also, you do not need to use the
new keyword to create a struct.
- Are value types
- Do not support inheritance
- Can implement interfaces
- Are stack-allocated
- Cannot be null
What is a class?
A class is unit that lets us organize code. The unit aims to be logical and modular. As a result, it's much easier to maintain and reuse code.
A class is the most common reference type in object-oriented programming (OOP).
Classes are designed to encapsulate the data. To access the data, the outside world must call the built-in functions, known as methods.
Inside a class, we have fields, which are member variables.
Are structs faster than classes?
Structs are faster than classes when storing small amounts of data (under 16 bytes).
It's because of the way they allocate memory:
- Classes are allocated on the heap and garbage-collected.
- Structs are allocated on the stack or inline and deallocated when the stack unwinds or the containing type is deallocated.
Structs being stored on stack means cheaper allocations and deallocations than classes.
However, classes may be faster than structs when working with large objects or when the object needs to be frequently boxed and unboxed.
When we box a value, it creates a new object on the heap and copies the value into it. The new allocation might lead to performance degradation. So if you decide to use structs, pay attention to boxing.
ref's to improve performance
To improve the performance of structs we can pass them by reference using the
ref keyword. That way, when we pass a struct to a method, we pass a reference rather than a copy.
When to use a struct over classes?
Structs are ideal for simple structures and light data carriers. Classes are ideal for defining complex behavior.
- What is the primary responsibility of this type? Is it data storage?
- Is its public interface defined entirely by properties that access or modify its data members?
- Will this type ever have subclasses?
- Will this type need polymorphism?
Josip Miskovic is a software developer at Americaneagle.com. Josip has 10+ years in experience in developing web applications, mobile apps, and games.Read more posts →
I've used these principles to increase my earnings by 63% in two years. So can you.
Dive into my 7 actionable steps to elevate your career.