C# 在一行中初始化并返回交错数组

C# 在一行中初始化并返回交错数组,c#,arrays,multidimensional-array,jagged-arrays,C#,Arrays,Multidimensional Array,Jagged Arrays,目前我正在这样做 public int[][] SomeMethod() { if (SomeCondition) { var result = new int[0][]; result[0] = new int[0]; return result; } // Other code, } 现在我只想返回[0][0]的空锯齿数组。有没有可能把三行减为一行。我想实现这样的目标 public int[][] SomeMe

目前我正在这样做

public int[][] SomeMethod()
{
    if (SomeCondition)
    {
        var result = new int[0][];
        result[0] = new int[0];
        return result;
    }
    // Other code,
}
现在我只想返回[0][0]的空锯齿数组。有没有可能把三行减为一行。我想实现这样的目标

public int[][] SomeMethod()
{
    if (SomeCondition)
    {
        return new int[0][0];
    }
    // Other code,
}
有可能吗?

请看这里和下面

您将需要创建一些辅助函数,然后它将成为一个单行程序


(也在寻找一种单线解决方案。)

通过返回锯齿数组的值,它会给您一些不明确的结果,如果您想返回锯齿数组的某个特定索引的某个特定值,可以通过将它们赋给变量来返回

 public static int  aaa()
    {

        int[][] a = new int[2][] { new int[] { 1, 2 }, new int[] { 3, 4 } };
        int abbb=a[0][0];
        Console.WriteLine(a[0][0]);
        return abbb;
    }

以下代码将返回1,因为这是锯齿数组的第一个元素。在一般情况下,您可以让编译器为您计算元素数:

    public int[][] JaggedInts()
    {
        return new int[][] { new[] { 1, 2, 3 }, new[] { 4, 5, 6 }, new[] { 7, 8, 9, 10 } };
    }
或者,如果希望它非常紧凑,请使用表达式体:

 public int[][] JaggedInts() => new int[][] { new[] { 1, 2, 3 }, new[] { 4, 5, 6 }, new[] { 7, 8, 9, 10 } };
因为您要的是一个空的锯齿数组,所以您已经有了它:

var result = new int[0][];
问题的下一行将抛出运行时异常,因为[0]是数组中的第一个元素,其长度必须为1个或多个元素

 result[0] = new int[0];  // thows IndexOutOfRangeException: Index was outside the bounds of the array.
以下是我认为您在一行中要求的内容:

public int[][] Empty() => new int[0][];