[Proposal]: Value Class #4026
Replies: 4 comments 14 replies
-
Moved to a discussion. This would need a lot of explanation on how this would actually work, esp. given the existing capabilities and featureset of the .net runtime. Thanks! :) |
Beta Was this translation helpful? Give feedback.
-
And the known limitations of inheritance of value types. There's a reason .NET doesn't permit allocating classes on the stack, it creates a lot of problems which are evident in the languages that do permit it, like C++. It becomes impossible to know the actual size of the object since it can be a derived type, but the compiler and runtime must know the actual size of the object. Otherwise you will invariably run into decapitation and shearing scenarios. It becomes impossible to something as simple as indexing to a specific element in an array because you have no idea how large each element might be. |
Beta Was this translation helpful? Give feedback.
-
I really don't understand what you're actually proposing here?
You're saying you want something that has the same behaviour as a struct and is allocated on the stack. Isn't that just a struct? |
Beta Was this translation helpful? Give feedback.
-
In my opinion this issue is really asking for two things in one: Stack-allocated objects and struct inheritance, but I don't see any reason the two features need to be combined. The runtime has basic support for automatically stack-allocated objects. It assumes all objects passed as parameters will escape though it's not extremely useful yet. The general issue covering stack-allocated objects is dotnet/runtime#11192 (See also: dotnet/runtime#4584 ) For struct inheritance, see the previous discussion at #936 |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Value Class
Summary
Value class that make behavior of class the same as struct, but instead of similar feature records Value Class allocated on stack
Motivation
Make C# more value oriented for performance benefits
Detailed design
For class
MyCustomClass
all fields allocated on the stack including fields of base classes in hierarchyDrawbacks
For class with deep inheritance chain could use to many memory on stack
Alternatives
Unresolved questions
Design meetings
Beta Was this translation helpful? Give feedback.
All reactions