如何在 C# 中显示空数组?

How to display null array in C#?

我在 中使用相同的代码,但现在我正在尝试调试类似

的错误

System.ArgumentNullException: 'Value cannot be null. (Parameter 'values')'

当我做的时候

static void Main(string[] arg){
     int[] numbers = new int[] { 2, 4 };
     Console.WriteLine(String.Join(",", HowSum(7, numbers)));

}

HowSum() returns 为 NULL 时如何解决此问题?

原文post供参考:

class HowSumSlow {

    static int[] HowSum(int targetSum, int[] numbers)
    {
        int[] empty = new int[] { };
        if (targetSum == 0) return empty;
        if (targetSum < 0) return null;

        
        
        foreach( var num in numbers){
            var remainder = targetSum - num;
            int[] remainderResult = HowSum(remainder, numbers);

            if (remainderResult != null){
                return remainderResult.Append(num).ToArray();
            }
        }
        return null;
    }
    static void Main(string[] arg) {
        int[] numbers = new int[] { 2, 3, 5 };
        Console.WriteLine(String.Join(",", HowSum(8, numbers)));
    }
}

How can I fix this when HowSum() returns a NULL?

您可以使用 ??null 数组指定回退:

Console.WriteLine(String.Join(",", HowSum(7, numbers) ?? Array.Empty<int>()));

如果 HowSum returns null.

,现在您正在将空的 int-array 传递给 String.Join

只需定期进行 null 检查以查找函数 returns 是否为 null

if (HowSum(8 , numbers) != null) {
        Console.WriteLine(String.Join(",", HowSum(8, numbers)));
    } else {
        Console.WriteLine("ITS NULLLLL");
    }

希望对您有所帮助。 :)

在我之前的回答(之后删除)中,我错过了递归。因此,您需要 null return 作为停止标准。

因此,负 targetSum 有效输入 而递归 但不是起始值.

所以,你可以做的是给它一个“入门方法”——就像这样:

// Your "HowSum" Method stays untouched!

static int[] StartHowSum(int targetSum, int[] numbers)
{
    if (targetSum < 0) 
    {
        throw new ArgumentOutOfRangeException (
            nameof(targetSum), targetSum,
            "Argument must be greater than or equal to 0."
            );
    }
    if (targetSum == 0) return Array.Empty<int>();
    // maybe also sanity-check `numbers`?

    int[] result = HowSum(targetSum, numbers);
    // Now that we checked input, is it possible to still get null OUTput?

    return result ?? Array.Empty<int>();
}

static void Main(string[] arg) {
        int[] numbers = new int[] { 2, 3, 5 };
        Console.WriteLine(String.Join(",", StartHowSum(8, numbers)));
    }

在考虑了大家的意见后,我发现最简单的方法是只存储结果并使用 ? 运算符。 (谢谢大家。我想在每条评论中都写下这一点,但显然我应该避免这样做。)

这是最终代码。

static int[] HowSum(int targetSum, int[] numbers)
    {
        int[] empty = new int[0];
        if (targetSum == 0) return Array.Empty<int>();
        if (targetSum < 0) return null;

        foreach (var num in numbers)
        {
            var remainder = targetSum - num;
            int[] remainderResult = HowSum(remainder, numbers);

            if (remainderResult != null){
                return remainderResult.Append(num).ToArray();
            }
        }

        return null;
    }
    static void Main(string[] arg)
    {
        int[] numbers = new int[] { 2, 4 };

        int[] result = HowSum(7, numbers);
        Console.WriteLine(result == null ? "null" : String.Join(",", result));

    }
}