Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why C# structs cannot be inherited? [duplicate]

Tags:

c#

.net

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...

like image 470
smwikipedia Avatar asked Feb 22 '10 10:02

smwikipedia


People also ask

Why C is the best language?

C is a general-purpose programming language and can efficiently work on enterprise applications, games, graphics, and applications requiring calculations, etc. C language has a rich library which provides a number of built-in functions. It also offers dynamic memory allocation.

Why C is being used?

The C programming language doesn't seem to have an expiration date. It's closeness to the hardware, great portability and deterministic usage of resources makes it ideal for low level development for such things as operating system kernels and embedded software.

Why is C used in C?

%d is used to print decimal(integer) number ,while %c is used to print character . If you try to print a character with %d format the computer will print the ASCII code of the character.

Why is C language named so?

Quote from wikipedia: "A successor to the programming language B, C was originally developed at Bell Labs by Dennis Ritchie between 1972 and 1973 to construct utilities running on Unix." The creators want that everyone "see" his language. So he named it "C". C is about the tone C.


1 Answers

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.

like image 153
11 revs, 4 users 71% Avatar answered Oct 02 '22 11:10

11 revs, 4 users 71%