string.Length vs string.IsNullOrEmpty(string)

0

Witam, Mógłby mi ktoś wytłumaczyć dlaczego w tym kodzie na SPOJu dostaje non-zero-exit-code (C#)

bool dataIsAvailable = true;
            
while (dataIsAvailable)
{
    var data = Console.ReadLine();
    if (data.Length > 0)
    {
        var arr = data.Split(' ').Select(x => Convert.ToDouble(x, System.Globalization.CultureInfo.InvariantCulture)).ToArray();

        var amount_of_zeros = arr[1] * arr[1] - 4 * arr[0] * arr[2];

        if (amount_of_zeros == 0)
        {
            Console.WriteLine("1");
        }
        else if (amount_of_zeros > 0)
        {
            Console.WriteLine("2");
        }
        else
        {
            Console.WriteLine("0");
        }
    }
    else
    {
        dataIsAvailable = false;
    }
}

A gdy zamienie

if (data.Length > 0)

na

if (!string.IsNullOrEmpty(data))

to już jest spoko?

Gdy data jest NULLem, to nie istnieje .Length, więc przy data.Lenght > 0 wyskakuje wyjątek? Dobrze rozumiem?

1

No, dobrze. Jeśli coś jest null, to nie istnieje, więc nie masz na czym wywołać metody/właściwości.

1 użytkowników online, w tym zalogowanych: 0, gości: 1