Click here to Skip to main content
15,949,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Why Structures are Sealed by default?
Posted

For the simple reason that you can't derive from any value type: only reference types.

The actual reason for this is complex, and has to do with what happens when you copy a value type: you get a bitwise clone, rather than a reference copy (because you don't have a reference to a value type). So if you could copy a derived struct via it's base type, you would get a base type bitwise copy, rather than a derived type bitwise copy. And now you would have lost information.

If you really feel the need to know, google for "Value slicing" and be prepared to get a small headache! :laugh:
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 2-Aug-11 3:03am    
Griff, the problem is not so easy as it seems. All you rational does not really work, because in some other languages extension of structures and other value types (including primitive types) works wonderfully.

Please see my answer.
This is a topic where no answer should be considered as "final" as there are too many factors including social and practical ones, not just pure rational.

--SA
 
Share this answer
 
because by default it is sealed No destructor for struct because structures are created in stack memory.
 
Share this answer
 
This problem is not so simple as it may look.

In my opinion, the real reason is some mistaken decisions in .NET architecture. (Actually, I consider the limitation of inheritance in .NET as its biggest problem and the lack of fully-fledged meta-classes (instead, there is a limited System.Type type) as the second biggest problem of .NET). To me, its' hard to explain as Anders Hejlsberg was the chief architect of Delphi.

I cannot accept rationale described by Griff as in some other languages extension of structures and other value types (including primitive types) works wonderfully.

—SA
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900