C# allows you to create an array without it being initialized with GC.AllocateUnitializedArray<T>
. T
can be any type, such as bool
or int
. However, I do not know of a way to do this to a 2D array, e.g. bool[,]
or int[,]
.
I know that I could allocate a 1D array that is the size of the 2D array, and then access it like it was a 2D array, like this:
int[] array = GC.AllocateUninitializedArray<int>(width * height); //width and height are width and height of the array
// ... (array is initialized)
int arrayAccessExample = array[x * height + y]; //x and y are anything greater than 0 and less than width and height, respectively
But that’s annoying to constantly do, and could easily lead to bugs if done wrongly. I could make the array access into a function, which would remove the potential for bugs, but that’s still inconvenient and I’d rather just be able to use a real 2D array. Is there a way to allocate an uninitialized 2D array directly, or to “cast” a 1D array into a 2D array?
CubeX is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Is there a way to allocate an uninitialized 2D array directly?
No, not possible at all. While the code is “kind-of” there internally in the AllocateArrayEx
function in gchelpers.cpp, the optional flags
parameter is not exposed by any FCall or QCall function. Therefore it’s not possible to do this even using reflection without recompiling the CLR.
You can and should open a GitHub issue requesting this feature, as it seems fairly simple to implement.
or to “cast” a 1D array into a 2D array?
Also no. They are completely different types, and even have entirely different allocation paths: SZArray (1D, zero-based) and other arrays.
I suppose in theory you could allocate a normal array, then do some horribly unsafe messing with the m_pMethTab
to replace it with a different one, and just hope it all “works”, but I wouldn’t use that in production.
But in my opinion, if your performance requirements are high enough to require uninitialized arrays then you probably don’t want normal 2D arrays anyway as they have a significant overhead, due to the lack of optimizations in the runtime for multi-arrays.
There is also the Span2D
type in the Microsoft.Toolkit.HighPerformance library, or you could create your own (a struct
would probably be faster than a class
like @Enigmativity has), to allow you to treat a 1D-array as 2D. You may still need to code carefully to avoid lots of unnecessary bounds-checking.
I would consider going down this path to make it easy for you:
public class UnallocatedArray<T>
{
public UnallocatedArray(int width, int height)
{
array = GC.AllocateUninitializedArray<T>(width * height);
this.Width = width;
this.Height = height;
}
private int Width { get; init; }
private int Height { get; init; }
private T[] array = null;
public T this[int x, int y]
{
get => array[x * this.Height + y];
set => array[x * this.Height + y] = value;
}
}
Then you’d be able to write:
var ua = new UnallocatedArray<int>(4, 5);
ua[2, 1] = 42;
Console.WriteLine(ua[2, 1]);
2