Array Allocations, references or whole objects


Let's say I have an array int[100], this would have to allocate

4 (4 bytes in an int) * 100 (length) = 400 bytes (plus a few bytes for type and length info).

Am I right?

However if I declare an array like: Vector3[100], this would allocate:

4*3 (4 bytes in a float, 3 floats in a Vector3) * 100 = 1200 bytes


But now to the real question, if I create an array of class objects. I have my custom class

public class MyClass {
   int x = 123;
   int y = 456;

And if I create an array with one 100 of those class objects, MyClass[100], would this allocate:

4*2 (two ints) * 100 = 800 bytes (plus a few bytes for type and length info)

or would it only allocate:

4 * 100 = 400 bytes

For the references to the MyClass objects, and the allocation for the MyClass objects would actually be done when calling new MyClass (...);

Thanks in advance, Aron

Classes are reference objects in C#, so it would only allocate the 400 bytes required to hold references. If your object had been a struct, it would have been a value type and would have allocated the full 800 bytes.