Skip to content

Max Array Length Property/Method #31366

Closed
@mikernet

Description

@mikernet

Background and Motivation

Maximum array length is often needed to find the maximum size that a buffer or collection can grow up to. dotnet/runtime has the magic constants for the maximum array length hardcoded in number of places for these purposes: https://github.com/dotnet/runtime/search?q=MaxArrayLength+0x7FEFFFFF

Proposed API

 namespace System
 {
     public class Array
     {
+        // Returns the maximum length of T[] that the runtime is able to allocate, assuming sufficient memory is available
+        public static int GetMaxLength<T>();
     }
 }

There are a lot of variables that factor into the max allowed array size (runtime implementation, 32 vs 64 bit, size of the data type, gcAllowVeryLargeObjects config, etc). Unless I'm missing something, there doesn't seem to be a particularly easy way to determine this at runtime for the current executing environment (please correct me if I'm wrong).

It would be rather helpful if there was a property or method akin to Array.GetMaxLength<int>() which returns the maximum length of an array of the given type in the current runtime environment.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions