Array of value-types

  • Thread starter Thread starter Rafal Gwizdala
  • Start date Start date
R

Rafal Gwizdala

Another question, related to my previous post:
Is array of value types a value type or a reference type?

From what I know it is a reference type in normal usage in .Net 1.0 and 1.1.
Do you know of any case where it would become a value type?
Maybe in .Net 2.0?


Best regards
Rafal Gwizdala
 
Rafal Gwizdala said:
Another question, related to my previous post:
Is array of value types a value type or a reference type?

From what I know it is a reference type in normal usage in .Net 1.0 and
1.1. Do you know of any case where it would become a value type?
Maybe in .Net 2.0?

An array of value types is still a reference type, yes.
 
Daniel O'Connell said:
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?
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?


Best Regards,
Rafal Gwizdala
 
Rafal Gwizdala said:
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 said:
Rafal Gwizdala said:
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.

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
 
John,
I don't understand your example. What is the intended difference between
(...)

I'm sorry, I misspelled your name in previous post - meant Jon, not John.

Rafal Gwizdala
 
I think Jon means you cannot use the base class for value
types(System.ValueType)
but rather use the actual value type directly to avoid boxing, similar to:
System.Object [] ints...;
ints [0] = 10; // Boxing

System.Int32 [] ints...;
ints [0] = 10; // No Boxing
 
Rafal Gwizdala said:
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?

Yes, but System.ValueType isn't (somewhat confusingly).
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.

That depends on what way they're stored on the heap. If they're stored
as part of another object (including an array), there's no need to box
them. If they're stored as a separate object in themselves, that's when
they become 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.

See http://www.pobox.com/~skeet/csharp/memory.html - hopefully that
will help your understanding.
 
Back
Top