I am reading the C# specification. I could use clarification on a segment:
C# has a unified type system. All C# types, including primitive types
such as int and double, inherit from a single root object type. Thus,
all types share a set of common operations, and values of any type can
be stored, transported, and operated upon in a consistent manner.
Furthermore, C# supports both user-defined reference types and value
types, allowing dynamic allocation of objects as well as in-line
storage of lightweight structures.
What does “in-line storage of lightweight structures” mean in this context?
Svick’s answer is good but I thought I would add a few extra points.
First off, the paragraph is flawed. Pointer types do not inherit from object. Values that are at compile time known to be interface types or type parameter types will, at runtime, be either invalid references or bona-fide instances of something that inherits from object, but it has always struct me as odd to say that these types “inherit” from object; inheritance is the property that members of the ancestor are members of the descendent, but you don’t normally think of “ToString” as being a member of IEnumerable. You think of it as being a member of the thing that implements IEnumerable.
The paragraph is also flawed because this is the only place “primitive type” appears in the spec, and it appears without definition. It is therefore both unnecessary and confusing and should be removed.
I’ve wanted this paragraph to be fixed for a while. Next time I see Mads I’ll remind him.
To address your specific question: svick is of course correct but it is helpful to see a specific example. When you say:
struct ColorfulInt
{
int value;
Color color;
...
}
and you create, say, an array:
ColorfulInt[] x = new ColorFulInt[100];
Then the storage for those 100 ints and 100 Colors goes in the array itself. If ColorfulInt were instead a class then the array would contain 100 references to ColorfulInt, each one of which would have to be individually allocated. Individually allocating those hundred elements is much less efficient in both time and space than simply allocating the storage right in the array itself.
2
It means that value types are stored directly where you define them, which makes them more efficient when compared with reference types.
What exactly does that mean? If you have a local variable of a value type it will be usually stored directly on the stack (but there are many exceptions). If you have a field of a value type, it will be stored directly in the enclosing class or structure.
1