Re: Two questions about C# datatypes
- From: Lasse Vågsæther Karlsen <lasse@xxxxxxxxxxx>
- Date: Tue, 12 Feb 2008 14:03:38 +0100
Piotrekk wrote:
Hi
1. int is value type so it is stored on stack. class is reference type
and it is stored on heap.
What is where when we declare class which contains sturct which
contains int. I suppose that even in this example ALL value types are
on stack. Otherwise I wouldn't understand it at all since visual
studio C# project puts evertyhing into a class. Since classes are ref
types I assume that everything that is in the class and derives from
object goes to heap. Is that true?
2. How exactly compiler detects type if an object such that it can
detect wrong casts? I would appreciate also any links.
Kind regards
Piotr Kolodziej
Value types in classes is stored inside the class. Reference types in classes stores a reference in the class to the object on the heap.
For your example, a class containing a struct will create an object where the struct is part of the memory allocated to the object. The int inside the struct is thus inside the struct.
In other words, like this:
reference -> [ Object ---- [ Struct --- [ Int32 ] --- ] ---- ]
An object has a hidden pointer to a structure containing information about the class it has been created from. That's how GetType() can get the right Type object, how "is" and "as" works, etc.
--
Lasse Vågsæther Karlsen
mailto:lasse@xxxxxxxxxxx
http://presentationmode.blogspot.com/
PGP KeyID: 0xBCDEA2E3
.
- References:
- Two questions about C# datatypes
- From: Piotrekk
- Two questions about C# datatypes
- Prev by Date: Re: disassembly
- Next by Date: Re: Garbage collectable pinned arrays!
- Previous by thread: Re: Two questions about C# datatypes
- Next by thread: static code analysis
- Index(es):
Relevant Pages
|