Click here to Skip to main content
15,902,938 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:
Recently in one of the interviews, I was asked a question about csharp arrays as following:

//an array of integers is defined

var arr = int a[100];

now, the question is - is this array(arr) stored as one object (of 100 integers) or 100 objects within memory ?
Posted
Comments
[no name] 27-Jul-13 22:01pm    
This, http://msdn.microsoft.com/en-us/library/vstudio/2z4khca9.aspx, may or may not answer you.
K.R.T 27-Jul-13 22:52pm    
Thanks for the link which you gave, however, the question still remains unanswered as the link only states that the arrays are treated like objects in Csharp, however whether the array in my question will be considered as one object or 100 objects, still remains a question.
Ron Beyer 27-Jul-13 22:06pm    
I hope that's not a direct copy from the question, its a syntax error...
Sergey Alexandrovich Kryukov 27-Jul-13 23:33pm    
Right... :-)
I tried to answer, but OP is recommended to RTFM instead. (For those who is unaware: this abbreviation means "Read The Following Manual..." ;-)
—SA
Ron Beyer 27-Jul-13 23:34pm    
That's the tame meaning of RTFM :)

First of all, there is no such thing as "C# array"! This is a .NET array. You need to understand the role and main ideas of .NET (before applying for a job as .NET developer).

It could be correctly written as
C#
int[] array = new int[100];


That creates an array allocating required memory on heap and array variable or some class/structure member becomes a reference to the array object. The array object itself does store 100 primitive int objects, 100 integers, not 100 references to something else. (You see, the question refers the term "object", and the understanding of it depends on some culture or conventions. In this context, I traditionally call "object" everything which exists during runtime and takes a place in memory, not only OOP "objects".)

But this is not all. Array itself is not a primitive object, it's an object-oriented reference type, so its instance holds something else, which is partially implementation-dependent. You can see what it is on your system if you inspect the variable under the debugger. Quite apparently, it, in particular, stores such an important piece of data as the size of the array object.

—SA
 
Share this answer
 
Comments
Ron Beyer 27-Jul-13 23:34pm    
You typed faster than I did, +5 :)
Sergey Alexandrovich Kryukov 27-Jul-13 23:51pm    
Than you, Ron. (I don't believe that; I just started earlier...)
—SA
Its confusing, not only because of the syntax errors, but technically the answer is both.

If you are talking about how its treated in memory, its a single continuous block of 100 integers represented in memory. This is apparent by how the Array.Resize[^] method works. If it was 100 individual "objects" then the entire array would not have to be reallocated when it is resized.

It is, however, 100 individual integers in memory, so in that respect its not a single "object". Arrays all derive from the System.Array class, even how you defined it above, so that's a single object. Its basically a single object that holds a collection of objects, so the answer technically is both. System.Array encapsulates the 100 individual items.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 27-Jul-13 23:38pm    
"...the answer technically is both" is merely the indication of the fact that the question is incorrect, which was good to point out. My 5.
I pointed out another incorrectness in my answer...
—SA
K.R.T 27-Jul-13 23:40pm    
Thanks Ron Beyer,

I believe the interviewer was looking for the statement "a single object that holds a collection of objects" as an answer.

Appreciate your replying to the question humbly.
Ron Beyer 27-Jul-13 23:45pm    
Careful with terminology though, I was a little loose with it. The term "collection" typically refers to mutable collections, arrays are immutable in the respect to how many objects it contains, which is why Resize has to make a new array.
K.R.T 27-Jul-13 23:48pm    
Very aptly said Ron !

Thanks,

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900