3

Here's the code:

int[,] Arr;
Arr = Array.Empty();

And here's the compile error for the second line:

Error CS0411 The type arguments for method 'Array.Empty()' cannot be inferred from the usage. Try specifying the type arguments explicitly.

This should be a simple syntax fix, but I haven't been able to get it.

6
  • int[,] Arr = new int[0, 0];. Commented Aug 24, 2022 at 1:02
  • Why do you want to use Array.Empty, and not new int[0, 0]? Commented Aug 24, 2022 at 1:05
  • This compiles: int[][,] Arr2; Arr2 = Array.Empty<int[,]>(); So, I'm expecting a simple syntactic answer. I might be wrong to expect that, though. Commented Aug 24, 2022 at 1:15
  • @HalHeinrich That's not a two-dimensional array. It's a jagged array (i.e., an array of arrays) where the inner array is a two-dimensional array. The simple syntax is either new [0, 0] or creating your own custom method. There's nothing built-in for what you're trying to do. FYI, the way Array.Empty() method works for one-dimensional arrays under the hood is also by returning new T[0]. There's no magic here. Commented Aug 24, 2022 at 1:16
  • 2
    "I might be wrong to expect that". You are. That is creating a 1D array where the type of the elements is a 2D array. The code you showed doesn't create any 2D arrays. It just creates a 1D array where each element can be a 2D array. A factory that makes egg cartons doesn't produce any eggs. Commented Aug 24, 2022 at 1:22

1 Answer 1

7

You may use:

int[,] Arr = new int[0, 0];

Or if you'd like to replicate the Empty() method for multidimensional arrays, you could write a helper class for that:

static class MultiDimArray
{
    public static T[,] Empty2D<T>() => new T[0, 0];

    public static T[,,] Empty3D<T>() => new T[0, 0, 0];

    // Etc.
}

Usage:

int[,] Arr = MultiDimArray.Empty2D<int>();

To take this a step further (and actually make it useful), we can make this work exactly like Array.Empty(), and thus avoid unnecessary memory allocation by using static readonly fields so that they're only initialized once:

static class MultiDimArray
{
    public static T[,] Empty2D<T>() => Empty2DArray<T>.Value;
    public static T[,,] Empty3D<T>() => Empty3DArray<T>.Value;
    // Etc.

    private class Empty2DArray<T> { public static readonly T[,] Value = new T[0, 0]; }
    private class Empty3DArray<T> { public static readonly T[,,] Value = new T[0, 0, 0]; }
    // Etc.
}
Sign up to request clarification or add additional context in comments.

4 Comments

main advantage of ArrayEmpty<T>() method is that it reuses same object. Your variant creates new instances on each call.
@Pavel You probably didn't read the last paragraph and the following code block :)
Indeed, my bad =)
Minor follow on for those interested in the CLR's one dimensional implementation: class EmptyArray<T> is currently defined at Array.cs#L1062.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.