Ehsan.Da Ehsan.Da - 2 months ago 26
C# Question

remove null items from a multidimensional array in C#

Consider the following array:

object[,] output = new object[5,3]{{"A",false,123},{"B",false,12},{"C",true,456},{null,null,null},{null,null,null}};

What is the right code if we are going to remove null items?


finally I solved my problem with multidimensional arrays by using a method as below:

public object[,] clear_array_nulls(object[,] input)
        int m = input.GetUpperBound(0);
        int n = input.GetUpperBound(1) + 1;
        object[] temp = new object[input.GetUpperBound(0)];
        for (int x = 0; x < m; x++)
            temp[x] = input[x, 0];
        temp = temp.Where(s => !object.Equals(s, null)).ToArray();
        object[,] output = new object[temp.Length, n];
        Array.Copy(input, output, temp.Length * n);
        return output;

thanks dude