PC Review


Reply
Thread Tools Rate Thread

Array of value-types

 
 
Rafal Gwizdala
Guest
Posts: n/a
 
      18th Apr 2005
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


 
Reply With Quote
 
 
 
 
Daniel O'Connell [C# MVP]
Guest
Posts: n/a
 
      19th Apr 2005

"Rafal Gwizdala" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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.


 
Reply With Quote
 
Rafal Gwizdala
Guest
Posts: n/a
 
      19th Apr 2005

"Daniel O'Connell [C# MVP]" <onyxkirx@--NOSPAM--comcast.net> wrote in
message news:(E-Mail Removed)...
>
> "Rafal Gwizdala" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> 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.


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


 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      19th Apr 2005
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
 
Reply With Quote
 
Rafal Gwizdala
Guest
Posts: n/a
 
      19th Apr 2005

"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



 
Reply With Quote
 
Rafal Gwizdala
Guest
Posts: n/a
 
      19th Apr 2005
> 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


 
Reply With Quote
 
Dennis Myrén
Guest
Posts: n/a
 
      19th Apr 2005
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

--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"Rafal Gwizdala" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
>> 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
>
>



 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      19th Apr 2005
Rafal Gwizdala <(E-Mail Removed)> wrote:
> 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.

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Storing and reading different types to/from an array Jay Microsoft C# .NET 10 17th Aug 2007 03:30 PM
Array Objects pointers, value types reference types?? plau011@hotmail.com Microsoft VB .NET 1 10th Feb 2006 12:32 AM
Dynamic array with strong types? Vincent Finn Microsoft C# .NET 11 12th Nov 2004 01:49 PM
Array of value types Dennis Myrén Microsoft C# .NET 10 6th Oct 2004 12:35 PM
How to build an (list)array of types G.Esmeijer Microsoft C# .NET 4 1st Apr 2004 10:33 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:40 PM.