"Jon Skeet [C# MVP]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Rafal Gwizdala <(E-Mail Removed)> wrote:
>> > An array of value types is still a reference type, yes.
>>
>> Thanks. That's what I expected, unfortunately.
>> And if you have an array of structs, how they are kept in the arrray?
>> I mean, are they boxed and stored as reference types in the array, or
>> placed
>> as value types in a contiguous memory area?
>
> The latter.
>
>> With simple (builtin) value types, it would be the latter case. But with
>> structs I'm afraid that the framework boxes them, so there's quite a lot
>> of
>> memory allocation going on (and there's a significant overhead in memory
>> usage). Am I right?
>> What about .Net 2.0 - will it change this behavior?
>
> Nope - value types are kept in the most obvious way. That's assuming
> the array has the right type, of course. If you declare an array like
> this:
>
> ValueType[] x = new ValueType[10];
>
> and then put values into it, those values *will* be boxed.
>
> Using
>
> SomeStruct[] y = new SomeStruct[10];
>
> won't box the values though.
>
> --
> Jon Skeet - <(E-Mail Removed)>
> http://www.pobox.com/~skeet
> If replying to the group, please do not mail me too
John,
I don't understand your example. What is the intended difference between
ValueType[] x = new ValueType[10];
and
SomeStruct[] y = new SomeStruct[10];
SomeStruct is a value type, isn't it?
My understanding of .Net is that value types aren't boxed only when
allocated on stack. And when they are allocated on heap, they will be boxed.
So using value types (structs) does not make too much sense when they are
stored on the heap...
Please correct me if I'm wrong.
Best Regards
Rafal Gwizdala