Re: Array Fundamentals
From: Rahul Anand (rahulanand_bis_at_rediffmail.com)
Date: 04/19/04
- Next message: Éric Moreau: "Re: Determine whether App is running is Visual Studio"
- Previous message: Dmitriy Lapshin [C# / .NET MVP]: "Re: Determine whether App is running is Visual Studio"
- In reply to: Abhishek Srivastava: "Re: Array Fundamentals"
- Next in thread: Jon Skeet [C# MVP]: "Re: Array Fundamentals"
- Messages sorted by: [ date ] [ thread ]
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. >> >> >> >. >
- Next message: Éric Moreau: "Re: Determine whether App is running is Visual Studio"
- Previous message: Dmitriy Lapshin [C# / .NET MVP]: "Re: Determine whether App is running is Visual Studio"
- In reply to: Abhishek Srivastava: "Re: Array Fundamentals"
- Next in thread: Jon Skeet [C# MVP]: "Re: Array Fundamentals"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|