Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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?

share|improve this question

2 Answers 2

up vote 10 down vote accepted

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.

share|improve this answer
    
So, is the importance in the memory abstraction of those objects, where when you're ready to manipulate them they are easier to access if they are in one contiguous block of addressable space verses pointers to who knows where? Is that correct, or am I still missing something? –  ChuckT Jan 19 '13 at 22:30
    
@ChuckT: Correct. You don't pay the overhead of the indirection, and you also get nice cache locality. –  Eric Lippert Jan 19 '13 at 23:57

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.

share|improve this answer
1  
That makes sense. Thanks for fleshing that out for me, svick! –  ChuckT Jan 18 '13 at 20:27

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.