string[] numbers = new string[5];
numbers[0] = "534";
numbers[2] = "115";
numbers[3] = "";
A 5-element sequence of numbers has been defined above, and then some of its elements have been assigned a value. Elements 0 and 2 were assigned a value, element 3 was determined as empty , elements 1 and 4 were left as null , that is, no assignment was made.
Let's try different methods to put "-" for empty elements while printing all the elements of the array into the label:
foreach (string in numbers)
{
if ( i == null ) label1.Text += "-\n";
else label1.Text += i + "\n";
}
The above loop only allows "-" to be set for null elements. In other words, "-" will be put for elements 1 and 4.
foreach (string in numbers)
{
if ( i=="" ) label1.Text += "-\n";
else label1.Text += i + "\n";
}
The above loop only allows "-" to be set for elements that are empty. In other words, "-" will be put for element 3.
foreach (string in numbers)
{
if (i == null || i=="") label1.Text += "-\n";
else label1.Text += i + "\n";
}
Here, "-" will be set for null or empty elements, that is, "-" will be put for elements 1, 3 and 4.