Why C structs cannot be inherited?
Object-Oriented Design practice on Codemia
Turn requirements into classes, and defend the design, on the problems that come up in OOD rounds.
C# is a powerful and versatile programming language developed by Microsoft, used extensively in both desktop and web applications. One of the unique features of C# is its support for both reference types and value types. This distinction allows developers to select the optimal data structure for their needs. A particularly important value type in C# is the struct. However, one common question regarding structs is: why can't they be inherited? This article delves into this question, providing technical explanations, examples, and insights to elucidate this design choice in C#.
Understanding C# Structs
In C#, a struct is a value type that is typically used to encapsulate small groups of related variables, such as coordinates, complex numbers, or RGB colors. Structs are similar to classes but with significant differences in functionality and performance characteristics.
Key Characteristics of Structs:
- Value Type: Unlike classes which are reference types, structs are stored on the stack, making them more memory efficient for small objects.
- No Inheritance: Structs do not support inheritance, which differentiates them from classes.
- Lightweight and Immutable: Structs are ideal for lightweight data structures and often used in scenarios demanding immutability.
- Direct Memory Allocation: Being stored on the stack instead of the heap, structs are directly allocated memory. This allows for quick variable accesses and reduces garbage collection overhead.
Why Can't C# Structs Be Inherited?
The inability of C# structs to support inheritance is a deliberate design choice tied to their nature as value types. Here are the technical explanations:
1. Value Type vs Reference Type
Structs in C# are value types, meaning they are copied by value rather than by reference. This copying behavior is integral to their function. When a struct is copied, all its data is copied into a new instance. Inheritance usually involves shared behaviors and data through references, which is incompatible with the copying semantics of value types.
Example:
Related reading
- Why CQRS and Event Sourcing design pattern is getting popular recently?
- Why do Python classes inherit object?
- Why do Python classes inherit object?
- Why does does it really? ListT implement all these interfaces, not just IListT?
- Why can I initialize a List like an array in C?
- Why can the C HttpClient not call this URL always times out?
- Why doesn't C infer my generic types?
- Why doesn't Java allow generic subclasses of Throwable?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Object-Oriented Design practice on Codemia
Turn requirements into classes, and defend the design, on the problems that come up in OOD rounds.