Click here to Skip to main content
14,385,111 members

Inherit a struct in C#? Why (can)not?

Rate this:
4.51 (21 votes)
Please Sign up or sign in to vote.
4.51 (21 votes)
18 Feb 2015CPOL
Understanding structs - why C# does not allow a struct to be inherited

Introduction - Why Use Structs Anyway?

Why struct anyway?
We all like C#'s structs very much [an idea alien to Java, apart from primitive types]. Structs, when they do not have to be boxed frequently, provide a nice way to handle relatively small and short lived data.

Allocations and deallocations of value types are in general cheaper than allocations and deallocations of reference types, since structs are allocated either on the stack or inline in containing types and deallocated when the stack unwinds or when their containing type gets deallocated, while reference types are allocated on the heap and garbage-collected.

Hmmm, It Won't Let Me Inherit....

Object Oriented Programming gives us the ability to do quite a lot of stuff, and the basic concept in OO is of course the inheritance.

Many of us have tried at one point to inherit a struct, only to find out that C# does not allow it.

Consider this code for example:

// What on earth can be wrong with this simple code? 
// NOTE: DateTime is a struct, if you didn't already know it, Now() is a good time to find out :)
struct MyDateTime : DateTime
    double SecondsFromMyBirthday { get; private set; }

// or with this fairly reasonable inherit request:
struct Point
   int x, y;

struct Point3D : Point
   int z;

Image 1

An honest programmer wishes to expand the DateTime struct, why shouldn't she?

This code will produce the compile time error: Type 'DateTime' in interface list is not an interface (the reason for this specific error message is the fact that since inheritance is not allowed the compiler expects only an interface after the colon symbol - it does not even take a misuse of inheritance into account...)

If the struct would be replaced to class, the compile time error will produce: 'MyDateTime': cannot derive from sealed type 'System.DateTime'

We understand then, that System.DateTime is (implicitly) sealed. But why?

Note: Actually the fact is that all struct types implicitly inherit from the class System.ValueType, which, in turn, inherits from class object.

Note: Although a Struct cannot be inherited, it can implement an interface.

The Answer

A struct has a fixed size allocated already on the stack.

Now consider the Point / Point3D example above which seems like a nice innocent case. Consider the following line of code:

// Assume we have a p2d which is a Point, 
// and a p3d which is a Point3D
p2d = p3d; 

What should happen on the assignment line then? A memory expansion of p2d on the stack to support the assignment?

Furthermore - structs, being a value type of course, do not use references (unless boxed), this would make polymorphism somewhat meaningless since there is no indirection via a reference pointer.

Also, consider the case of arrays - arrays of value types are stored "inline" (for performance and garbage collection reasons). If we use a struct without knowing it is actually a "derived struct", it will cause memory corruption.

In conclusion - we've seen why it is highly unreasonable for .NET to allow inheritance of a struct. The good news is that you can always inherit ... well ... a class!


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Joezer BH
Chief Technology Officer
United States United States
Senior dish washing consultant for over 25 years!

Comments and Discussions

SuggestionThe article requires significant changes. Pin
Pavel Evgenjevich Timoshenko1-Apr-15 4:29
memberPavel Evgenjevich Timoshenko1-Apr-15 4:29 
QuestionIntersting one Pin
Sacha Barber25-Feb-15 23:56
mvaSacha Barber25-Feb-15 23:56 
AnswerRe: Intersting one Pin
Joezer BH26-Feb-15 3:11
professionalJoezer BH26-Feb-15 3:11 
QuestionWhat do these 2 statements mean ? Pin
Bilal Fazlani23-Feb-15 18:47
memberBilal Fazlani23-Feb-15 18:47 
AnswerRe: What do these 2 statements mean ? Pin
Joezer BH23-Feb-15 21:19
professionalJoezer BH23-Feb-15 21:19 
QuestionStructs, why use? Pin
KP Lee23-Feb-15 1:06
memberKP Lee23-Feb-15 1:06 
QuestionMy vote of 5 Pin
Abhinaw Kumar19-Feb-15 19:56
professionalAbhinaw Kumar19-Feb-15 19:56 
AnswerRe: My vote of 5 Pin
Joezer BH21-Feb-15 21:00
professionalJoezer BH21-Feb-15 21:00 
GeneralMy vote of 5 Pin
_Vitor Garcia_19-Feb-15 4:38
member_Vitor Garcia_19-Feb-15 4:38 
GeneralRe: My vote of 5 Pin
Joezer BH19-Feb-15 5:11
professionalJoezer BH19-Feb-15 5:11 
GeneralMy vote of 5 Pin
Humayun Kabir Mamun18-Feb-15 20:51
memberHumayun Kabir Mamun18-Feb-15 20:51 
GeneralRe: My vote of 5 Pin
Joezer BH18-Feb-15 21:10
professionalJoezer BH18-Feb-15 21:10 
QuestionThankfully we have extension methods Pin
Eric Lynch18-Feb-15 13:30
mvaEric Lynch18-Feb-15 13:30 
GeneralMy vote of 5 Pin
Afzaal Ahmad Zeeshan18-Feb-15 7:07
mveAfzaal Ahmad Zeeshan18-Feb-15 7:07 
GeneralRe: My vote of 5 Pin
Joezer BH18-Feb-15 20:08
professionalJoezer BH18-Feb-15 20:08 
GeneralRe: My vote of 5 Pin
Afzaal Ahmad Zeeshan19-Feb-15 5:24
mveAfzaal Ahmad Zeeshan19-Feb-15 5:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Posted 18 Feb 2015


14 bookmarked