I'm trying to write a method to that takes a 2 dimensional array and uses the null -coalescing operator to find nulls.It should then print how many null and how many non-null elements there are Here is the code
static void NullSearcherWithCoalecsingOperators(int?[,] TwoDArr)
{
int? NonNull = 0;
int? Null = 0;
for (int i=0;i<TwoDArr.GetLength(0);i++)
{
for (int j = 0; j < TwoDArr.GetLength(1); j++)
{
TwoDArr[i,j] = NonNull++ ?? Null++;
}
}
Console.WriteLine($"There are {NonNull} nonnull elements and {Null} null elements in this 2d array");
}
Trouble is that it always prints that everything is not null and nothing is null. I'm wondering why the ++ isn't working on the right side? Thanks
NonNullis null.int?will just result in null.var _ = TwoDArr[i,j] == null ? Null++ : NonNull++;but you'd have to changeNullandNonNulltointinstead ofint?.