I have a problem with understanding Value Types representation in .NET.
Each Value Type is derived from System.ValueType class, so does this mean that Value Type is a class?
For example, if i write:
int x = 5;
it means that i create an instance of System.Int32 class an’ write it into variable x??
The System.ValueType class is really just a “meta” class, and internally is handled differently than normal classes. Structs and primitive types inherit System.ValueType implicitly, but that type doesn’t actually exist during runtime, it’s just a way for assemblies to mark that a class should be treated like a value type struct and take on pass-by-value semantics.
Contrary to the other answers, value types are not always allocated on the stack. When they are fields in a class, they sit on the heap just like the rest of the class data when that object is instantiated. Local variables can also be hoisted into an implicit class when used inside iterators or closures.