Typical print of jagged arrays

I am currently working with a lot of arrays and for debugging purposes I wrote a generic method Print()

to print different types of arrays

static void Main()
{
    Print(new double[]{ 1, 2, 3 });

    // Output: [ 1, 2, 3 ]
}

static void Print<T>(T[] array)
{
    int size = array.Length;
    if (size == 0)
        return;

    string str = "[ ";

    for (int i = 0; i < size; i++)
    {
        str += array[i].ToString();

        if (i < size - 1)
            str += ", ";
    }

    str += " ]";

    Console.WriteLine(str);
}

      

which still works. Then I wanted to print an array of arrays, for example double[][]

, and tried the following:

static void Main()
{
    Print(new double[][]
    {
        new double[] { 1, 2 },
        new double[] { 3, 4 },
        new double[] { 5, 6 },
    });

    // Output: [ 1, 2 ]
    //         [ 3, 4 ]
    //         [ 5, 6 ]
}

static void Print<T>(T[] array)
{
    if (array.Length == 0)
        return;

    if (array[0].GetType().IsArray)
    {
        foreach (var element in array)
        {
            Print<T>(element);
        }
    }
    else
    {
        int size = array.Length;
        string str = "[ ";

        for (int i = 0; i < size; i++)
        {
            str += array[i].ToString();

            if (i < size - 1)
                str += ", ";
        }

        str += " ]";

        Console.WriteLine(str);
    }
}

      

I just wanted to check if the elements are array

again arrays, and if so, I call the Print function again for each element

one array

. But it Print(element)

doesn't work because it element

has a type T

, not T[]

, and I don't know how to tell the compiler what T

is an array in this case . What do I need to do to make this work?

+3


source to share


2 answers


You must provide 2 overloads of your method Print

- you can call the 1D version from the 2D version as you like:

static void Print<T>(T[][] array)
{
     Console.WriteLine("Print 2D Array");
}
static void Print<T>(T[] array)
{
     Console.WriteLine("Print 1D Array");
}

      



Live example: http://rextester.com/LYBUN44476

+1


source


You can do this using dynamics:

    void Main()
    {
        Print(new double[][]
        {
            new double[] { 1, 2 },
            new double[] { 3, 4 },
            new double[] { 5, 6 },
        });

        // Output: [ 1, 2 ]
        //         [ 3, 4 ]
        //         [ 5, 6 ]
    }

    static void Print(dynamic array)
    {
        if (array.Length == 0)
            return;

        if (array[0].GetType().IsArray)
        {
            foreach (var element in array)
            {
                Print(element);
            }
        }
        else
        {
            int size = array.Length;
            string str = "[ ";

            for (int i = 0; i < size; i++)
            {
                str += array[i].ToString();

                if (i < size - 1)
                    str += ", ";
            }

            str += " ]";

            Console.WriteLine(str);
        }
    }

      



If you want to test less of the code - I suggest using LinqPad where you have AnyType.Dump () method and you don't have to implement anything;)

+1


source







All Articles