C# Struct vs Class: Key Differences πŸ”‘

By Josip Miskovicβ€’
A thumbnail showing code: C# Struct vs Class

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:

ClassStruct
Reference typeValue type
Support inheritanceNo inheritance
Can have null referenceCannot have null reference
Contain complex logicLight on logic, they structure data
Can have parameterless constructorsCan 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.

So, structs:

  • 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:

  1. Classes are allocated on the heap and garbage-collected.
  2. 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.

Use 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?

My take:

Structs are ideal for simple structures and light data carriers. Classes are ideal for defining complex behavior.

Consider:

  1. What is the primary responsibility of this type? Is it data storage?
  2. Is its public interface defined entirely by properties that access or modify its data members?
  3. Will this type ever have subclasses?
  4. Will this type need polymorphism?
Josip Miskovic
About Josip

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 β†’
Published on:
Download Free Software DeveloperΒ Career Guide

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.