Why C# structs cannot be inherited?

C#.Net

C# Problem Overview


I am reading CLR via C# by Jeffery Richter and it says a struct is a value type and cannot be inherited.

Are there any technical or philosophical reasons?

ADD 1 - 5:53 PM 11/11/2020

Every decision, no matter how reasonable or accidental it may look, has its impact, subtle or profound. We try to decouple things, sometimes by creating new coupling...

C# Solutions


Solution 1 - C#

Edit: There are serious editorial concerns about this post, apparently. See comment section.

A little of both.

Philosophically, it works out - there are classes, which are the "real" building block for object oriented programming, and there are structs, which are lightweight data types for storage but allow object-like method calls for familiarity and convenience.

Technically, being a "value type" means that the entire struct - all of its contents - are (usually) stored wherever you have a variable or member of that type. As a local variable or function parameter, that means on the stack. For member variables, that means stored entirely as part of the object.

As a (primary) example of why inheritance is a problem, consider how storage is affected at a low level if you allowed structs to have subtypes with more members. Anything storing that struct type would take up a variable amount of memory based on which subtype it ended up containing, which would be an allocation nightmare. An object of a given class would no longer have a constant, known size at compile time and the same would be true for stack frames of any method call. This does not happen for objects, which have storage allocated on the heap and instead have constant-sized references to that storage on the stack or inside other objects.

This is just an intuitive, high-level explanation - See comments and other answers for both expanded and more precise information.


Edit: The link in the comments to Eric Lippert's article The Stack Is An Implementation Detail, is now located on his personal blog site.

Solution 2 - C#

Because it is the way structs are represented in .NET. They are value types and value types don't have a method table pointer allowing inheritance.

Solution 3 - C#

You may find the answers to SO Question Why are .NET value types sealed? relevant. In it, @logicnp refers to ECMA 335, which states:

> 8.9.10 Value type inheritance > > - [...] > - Will be sealed to avoid dealing with the complications of value slicing. > - The more restrictive rules specified here allow for more efficient implementation without severely compromising functionality.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionsmwikipediaView Question on Stackoverflow
Solution 1 - C#Jesse MillikanView Answer on Stackoverflow
Solution 2 - C#Darin DimitrovView Answer on Stackoverflow
Solution 3 - C#Robert PaulsonView Answer on Stackoverflow