Click here to Skip to main content
15,888,301 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hello Friends,

I have few doubts on value types

1)Value types dont have a type object pointer. But still we can call the get type method and it returns the correct type. How is that possible, because if the type object is not present, the type of the object cant be determined.

So according to my thinking, the value type should be boxed. Am i correct?

2)Why are value types implicitly sealed?


I will be obliged if helped.


Thanks and Regards,

Rahul
Posted
Comments
johannesnestler 22-Jul-14 10:43am    
I see it like this: since ValueType is derived from object there are no real "value types" in .NET - ValueType is just a class handled special by the Compiler to give it value type semantics - in real it is a "pointer".
OriginalGriff 22-Jul-14 11:10am    
Not true - value types are primitives that are generally stack based rather than heap - in order to get a reference to a value type you have to box it as a reference type first.
johannesnestler 22-Jul-14 11:47am    
agreed -I'm wrong. But anyway: by calling GetType implicit boxing happens, dosn't it?Therefore if you ask a value type for it's type the compiler does box it to make type Information available.
Sergey Alexandrovich Kryukov 22-Jul-14 12:33pm    
Not only stack.
—SA
Rahul VB 23-Jul-14 2:25am    
Good which means i am almost there

1 solution

This may help: Using struct and class - what's that all about?[^] - it may explain a bit more than you wanted, but it should cover what you want.

Why are structs / value types sealed? So you can't inherit from them! :laugh:
I know, I know, that sounds like a justification, not a reason - but it's the reason.
If you could inherit from a value type, you could expand it in the derived class (and in fact you are likely to or there would be little point in deriving from it) which would mean one of two things:
1) The system would have to convert the value type to a class so that overridden and new methods worked properly,
Or
2) The system would have to process the inheritance chain for all objects, not just reference types. This could slow execution considerably compared to the "simplified" model it can use at present.

And in addition, expanding a value type would imply that it would grow beyond 16 bytes - the recommended maximum for a struct for performance reasons.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 22-Jul-14 11:54am    
The answer to the question #2 is absolutely not motivated. There are systems where inheritance of value-type structures was possible, and nothing slowed down (Turbo Pascal, Object Pascal). And of course nothing "converts value type to class". This is just you imagination that it could be done this way, but it has nothing to do with how it is really implemented.

Imagine simple thing: You have structure with members A, B. And another one has the same very A, B + C. And the structures are assignment-compatible, so A and B is always assigned, and C is not. What's so OOP here? What would need heap and classes? Nothing. Quite a trivial thing. If it is not available in Microsoft does not mean it is impossible or even impractical.

You just don't have the explanation. I don't have, too. I would say, right answer would be just one: because they decided to do so. Everything is just pseudo-logical explanations, which is much worse than no explanation.

Your item 1) and 2) are related to question #2 only. Question #1 is not answered, but it's hard to answer, just because the misconception it too confusing.

—SA

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