Home > Net >  How to display null array in C#?
How to display null array in C#?

Time:03-03

I am using the same code in my previous post but now I am trying to debug errors like

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

when I do

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

}

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

Here is the original post for reference:

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)));
    }
}

CodePudding user response:

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

You can use ?? to specify a fallback for the null array:

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

Now you are passing an empty int-array to String.Join if HowSum returns null.

CodePudding user response:

just do a regular null check to find if the function returns null or not

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

Hope it helped. :)

CodePudding user response:

In my previous answer (deleted since), I missed the recursion. So, you need the null return for the stop criterion.

Therefore, a negative targetSum value is a valid input while recursing but not as a start value.

So, what you could do, is to give it a "starter method" - like this:

// 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)));
    }

CodePudding user response:

After taking into account what everyone said, I found that the simplest way was to just store the result and use a ?-operator. (Thank you everyone. I wanted to write that in each and every comment, but apparently I'm supposed to refrain from that.)

Here's the final code.

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));

    }
}
  • Related