Re: Array Fundamentals

From: Rahul Anand (rahulanand_bis_at_rediffmail.com)
Date: 04/19/04


Date: Mon, 19 Apr 2004 02:55:16 -0700


Underlying Concepts:
===================

When a value-type object is created, C# allocates a single
space in memory, and puts the contents of the object into
it. Primitive types such as int, float, bool or char are
value types. When the runtime deals with a value type,
it's dealing directly with its underlying data and this
can be very efficient, particularly with primitive types.

With reference types, however, an object is created in
memory, and then handled through a separate reference -
rather like a pointer.

-------

Now as I understand all variables defined under a
reference type (for example class object), are stored on
Heap.

Only local variables of a function are allocated on Stack.

Therefore, the values inside an Array should be allocated
on Heap.

Check following links for further reference:
http://blogs.msdn.com/cbrumme/archive/2003/05/10/51425.aspx

--
Cheers,
Rahul Anand
>-----Original Message-----
>
>Thanks for your quick help.
>
> > *** Value types can be stack allocated, but they 
reside on the heap
> > when they are members/elements of an instance of a  
ref type.
>
>I don't understand this statement. How can a value type 
exist on the 
>heap. AFAIK, the main difference between a value type and 
a reference 
>type is that only the reference type exist on the heap.
>
>So how come value type exist on the heap in case of 
arrays?
>
>regards,
>Abhishek.
>
>Willy Denoyette [MVP] wrote:
>> Inline ***
>> 
>> Willy.
>> 
>> "Abhishek Srivastava" <abhishek-srivastava@nospam.net> 
wrote in message 
>> news:u4bxNZeJEHA.3120@TK2MSFTNGP09.phx.gbl...
>> 
>>>Hello All,
>>>
>>>In .Net Array is a reference type and an int is a value 
type. If I create 
>>>an array of int, then will the items inside the array 
get boxed?
>>>
>> 
>> *** No.
>> 
>> 
>>>If yes, it will be a terrible overhead. If no, then 
where will the array 
>>>elements exist?
>> 
>> *** On the heap, as they are elements of an array which 
is a ref. type.
>> 
>> since items inside the array are value type they have to
>> 
>>>be on the stack of the executing thread, but the array 
itself will be on 
>>>the heap since its a reference type.
>>>
>> 
>> *** Value types can be stack allocated, but they reside 
on the heap when 
>> they are members/elements of an instance of a  ref type.
>> 
>> 
>>>Thanks for your help in advance.
>>>
>>>regards,
>>>Abhishek. 
>> 
>> 
>> 
>.
>


Relevant Pages

  • Re: Array Fundamentals
    ... > when they are members/elements of an instance of a ref type. ... type is that only the reference type exist on the heap. ... >>In .Net Array is a reference type and an int is a value type. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Array Fundamentals
    ... How can a value type exist on the heap. ... > that only the reference type exist on the heap. ... reference to a heap allocated object of type "int array". ... The elements of the array are int's and their values are copied to the heap ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Array Fundamentals
    ... > In .Net Array is a reference type and an int is a value type. ... *** On the heap, as they are elements of an array which is a ref. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: undefined vs. undefined (was: new Array() vs [])
    ... or to the array performance, I dared to move it to a new thread. ... And section 10.1.3 explains the assignment of the undefined value to ... to give a Reference type. ... var arr = ...
    (comp.lang.javascript)
  • Re: GetCrossReferenceItems and sparse returns for wdCaptionTable
    ... The array that you get back from GetCrossReferenceItems is equivalent to the ... The Reference Type you must give it is equivalent to the ... wdCaptionFigure, wdCaptionEquation. ...
    (microsoft.public.word.vba.general)