473,624 Members | 2,258 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Array Fundamentals

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?

If yes, it will be a terrible overhead. If no, then where will the array
elements exist? 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.
Thanks for your help in advance.

regards,
Abhishek.
Nov 16 '05 #1
6 1302
n!
> 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, the reference is to the array itself not to the items contained.
If yes, it will be a terrible overhead. If no, then where will the array
elements exist? 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 are copied (by value) into each array element. Value types do
not *have* to exist on the stack they can be copied around freely.

n!
Nov 16 '05 #2
Inline ***

Willy.

"Abhishek Srivastava" <ab************ *****@nospam.ne t> wrote in message
news:u4******** ******@TK2MSFTN GP09.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.

Nov 16 '05 #3

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" <ab************ *****@nospam.ne t> wrote in message
news:u4******** ******@TK2MSFTN GP09.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.


Nov 16 '05 #4
"Abhishek Srivastava" <ab************ *****@nospam.ne t> wrote in message
news:eQ******** ******@TK2MSFTN GP11.phx.gbl...

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?


Instances of "Reference types" can ONLY exist on the heap.
Instances of "Value types" (the bits that make up the value) can exist on
both the stack and the heap.
Consider following sample:
void SomeMethod() {
int[] intArr = new int[] {1, 2, 3};
...
intArr is a local variable, as such it is stack allocated, and it holds a
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
allocated object space.

Stack Heap

IntArr ------->|Obj header |
|--------------|
| 1 |
|--------------|
| 2 |
|--------------|
| 3 |
|--------------|

Willy.
Nov 16 '05 #5

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/archiv.../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 theheap. AFAIK, the main difference between a value type and a referencetype 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" <ab************ *****@nospam.ne t>

wrote in message news:u4******** ******@TK2MSFTN GP09.phx.gbl...
Hello All,

In .Net Array is a reference type and an int is a value type. If I createan 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 arrayelements 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 onthe 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.


.

Nov 16 '05 #6
Abhishek Srivastava <ab************ *****@nospam.ne t> wrote:
> *** 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.


No. That's just an oversimplificat ion which confuses people,
unfortunately.

See http://www.pobox.com/~skeet/csharp/memory.html

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
575
by: Stormkid | last post by:
Hi Group I'm trying to figure out a way that I can take two (two dimensional) arrays and avShed and shed, and subtract the matching elements in shed from avShed I've pasted the arrays blow from a print_r cmd any suggestions would be great. Thanks much Todd //avShed array Array ( => Array ( => 1 => 08:00 ) => Array ( => 1 => 08:05 ) => Array ( => 1 => 08:10 ) => Array ( => 1 => 08:15 ) => Array ( => 1 => 08:20 ) => Array...
1
1495
by: Piotr Galuszkiewicz | last post by:
Hi! I'm looking for 6. edition of "Core Java 2 vol.1 - Fundamentals" (authors: Cay Horstman, Gary Cornell). I want to buy back used book from somebody. BUT(!) I'm looking for new, 6. edition (updated to Java SDK 1.4). If anyone has it and don't need, send me info please. Regards, Peter
0
1997
by: Frank Luff | last post by:
Does anyone have any experience with or recommendations on Oracle's CDROM for OCP DBA Fundamentals I? $600 is a lot to shell out sight unseen. Thanks for any comments. Frank
5
4528
by: Marcel Hug | last post by:
Hi NG ! I'm new in C# and I'm reading a book about the fundamentals and concepts. In the chapter Methods it's written to use virtual, if i would like to override the method in a subclass. This I've to do by using override. It's also written, that's possible to "hide" the base class method by using the new key word. Because I've already written some C# code and I didn't know anything
1
2632
by: vidu13 | last post by:
hi, I am going to appear for Exam# 1Z1-221:Oracle Inventory Management 11i Fundamentals which is the beta version. Is there anyone who has appeared for these exams who can give some tips on pattern and sample questions? Regards, Vidhya
8
1349
by: michael sorens | last post by:
I have written a few applications to read data from a database but now, as I turn my attention to writing to a database, I am apparently missing some fundamentals. I created a new database with a new (empty) table called Version. I then created a new data source in the Data Sources pane in Visual Studio 2005, yielding a DataSet named MyTestDataSet to reference the Version table. I then created a short WinForms program to just run ...
0
1313
by: nkechifesie | last post by:
Am reading for Fundamentals1 exam , would love to take it this month. Thats Oracle 9i fundamentals i and was wondering if there are any advice from you all.
2
3660
by: situ | last post by:
Hi, i took db2 fundamentals 730 exam on Dec-06 in one of the prometric centers, and succesfully passed also. but the problem is i didn't got my certificate yet, usually how long it takes to ship the certificate ? Thanks and regards
19
1828
by: Adam | last post by:
Hi, I'd like to return an (arbitrary length) string array from a function so that after calling the array I've got a list of strings I can access. After much perusing of the internet I found a related answer here (by Eric Sosman) which involved creating an array of pointers and using that, so it looks something like:
0
8170
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8619
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8334
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7158
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6108
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5561
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4173
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2604
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1482
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.