How to create an uninitialized 2D array?

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?

New contributor

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

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật