0

This seems like a basic question, but it's still bugging me.

Why doesn't

MyObject [] myobject = new MyObject [10];

allocate 10 objects? Why do we have to call new on each individual object?

myobject [0] = new MyObject();
:::
myobject [9] = new MyObject();

Or am I just making a silly mistake? :)

7 Answers 7

7

As far as I am aware, you arent creating 10 objects in the array, you are creating an array with 10 spaces for objects of type "My Object", null is a perfectly acceptable state for an item within the array though.

Sign up to request clarification or add additional context in comments.

Comments

2

You're calling the array constructor, which creates an array, not the object constructor ten times.

What constructor on the element type would you like to call? There's no way to provide any parameters as it is. The only constructors being called implicitly are value types (structs) like integers and doubles.

What if the element constructor fails? It could throw any exception. Should you get an half initialized array, or no array at all? Should the exception bubble or be hidden?

What if the element constructor is just very very slow. Add a Thread.Sleep() and the code that calls you would basically hang. How would you debug that?

And my last thought, why are you even using arrays when there's List<T>? =)

Comments

1

It's because it only allocates references to MyObject, not the object itself. That's the case with every non-primitive type in languages like C# or Java.

Comments

1

You could also use the following declaration style (not particularly useful in the following example, I would recommend a for loop instead)

MyObject[] myObject 
    = new MyObject[]
        {  new MyObject(), 
           new MyObject(), 
           new MyObject(), 
           new MyObject(), 
           new MyObject(),
           new MyObject(), 
           new MyObject(), 
           new MyObject(), 
           new MyObject(), 
           new MyObject()
        };

however, in this case it could be easier to type.

int[] myInts = new int[]{ 10,34,13,43,55,2,0,23,6,23 };

Comments

0

That's exactly how it should be done.

For example:

MyObject[] myObject = new MyObject[10]; // creates 10 null references

for (int i=0;i<myobject.Length;i++)
   myobject[i] = new MyObject();

Comments

0

Because all you have created is an array with 10 spaces that will contain your MyObject object instances. When will you put those in and in what state, it's up to you.

1 Comment

Emphatically, none of the elements in the array is an object instance. They are all references that can point -- or refer -- to object instances, or be null.
0

Looks like your talking about C# from the syntax.

More or less, in the CLR/.NET framework, an array is an array which contain's object's of type MyObject. Unlike C/C++, where an array is an array of objects of type MyObject.

i.e. C# array's are simply container's, conceptually they are they are distinct from the object's they hold. C/C++ (native memory), your pretty much cheating and forgo'ng the formality of having clear seporation of duties... your array is essentially the object it contains....

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.