C# arrays

Started by
2 comments, last by rip-off 14 years, 11 months ago
I am tested out c#. It seems as if in comparison to C and C++, you can't declare an array on the stack like this int array[100]; I have to do this instead int[] array; array=new int[100]; Why does it require dynamic memory allocation? [Edited by - V-man on May 3, 2009 3:22:56 PM]
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);
Advertisement
Basically, because arrays are reference types, not value types.
In C# arrays are reference types, they inherit from the base class Array, and like all reference types they are allocated on the heap rather than the stack.
Heap allocation doesn't have to be quite as bad in C# as C++ however. Modern managed languages can take advantage of a number of optimisations which can make dynamic allocation relatively cheap and garbage collection relatively quick for short lived objects.

This topic is closed to new replies.

Advertisement