Why does every line in the .txt-file output end with 'System.int32'? C#

你离开我真会死。 提交于 2021-02-11 12:40:17

问题


C#, Console Application, Virtual Studio 2015, Paralelles: Windows 8.1.

The code is as follows:

class txt_program
{
    public void txt()
    {
        /* 0 */
        int[] M_array_0 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
        /* 1 */
        int[] M_array_1 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
        /* 2 */
        int[] M_array_2 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
        // etc.

        // the M matrix
        int[][] M = { M_array_0, M_array_1, M_array_2, M_array_3, M_array_4,
        M_array_5, M_array_6, M_array_7, M_array_8, M_array_9, M_array_10,
        M_array_12, M_array_13 };

        // the time pick is beeing called into this item.
        Time_pick_2 T2 = new Time_pick_2();

        // the if loop making

        // 0
        if (T2.M_build_0() == true)
        {
            int nr_0 = 0;
            for (int i = nr_0; i < nr_0 + 2; i++)
            {
                M[1][i] = M_array_0[i] + 1;
            }
        }

        // 1
        else if (T2.M_build_1() == true)
        {
            int nr_1 = 1;
            for (int i = nr_1; i < nr_1 + 2; i++)
            {
                M[1][i] = M_array_1[i] + 1;
            }
        }

        // 2
        else if (T2.M_build_2() == true)
        {
            int nr_2 = 2;
            for (int i = nr_2; i < nr_2 + 2; i++)
            {
                M[2][i] = M_array_2[i] + 1;

            }
        }

        // etc.



        using (StreamWriter SW = new StreamWriter(@"txt.txt"))
            {
                // the loops creating the .txt-file.
                for (int i = 0; i < M.GetLength(0); i++)
                {
                    for (int a = 0; a < 13; a++)
                    {
                        SW.Write(" " + M[i][a]);
                    }
                    SW.WriteLine(M);
                }
            }
        }
    }
}

As pastebin:

http://pastebin.com/3HDYwzzZ

The output is given as:

0 0 0 0 0 0 0 0 0 0 0 0 0System.Int32[][]
0 1 1 0 0 0 0 0 0 0 0 0 0System.Int32[][]
0 0 0 0 0 0 0 0 0 0 0 0 0System.Int32[][]
// etc.

I checked some other post about the appearance off System.Int32[][] but in all of the cases it was code that didnot generate any output except System.Int32[], which meant that the code could not interpret the for-loopcreating the output. Here the correct output is displayed but still with the System.Int32[][] end. Why is this and what am I doing wrong?


回答1:


You are outputting the whole array after your inner loop. If you want a newline you could use Console.WriteLine("")

using (StreamWriter SW = new StreamWriter(@"txt.txt"))
{
    // the loops creating the .txt-file.
    for (int i = 0; i < M.GetLength(0); i++)
    {
        for (int a = 0; a < 13; a++)
        {
            SW.Write(" " + M[i][a]);
        }
        //SW.WriteLine(M); // <-- this line was generating your output
        SW.WriteLine("");
    }
}



回答2:


By default, the ToString method returns the type of the object. So, if you print a non-primitive type without overriding the ToString method (defined in Object class), you will get the type name instead.

The problem line in your case is:

SW.WriteLine(M);



回答3:


As Heinzbeinz already said you are printing an array which in C# wil result in printing the object type for example:

int[] ar1 = {0,1,2,3};
int[] ar2 = {0,1,2,3};
int[][] m = {ar1,ar2};

Console.WriteLine(m);

wil result in printing System.Int32[][]



来源:https://stackoverflow.com/questions/39569379/why-does-every-line-in-the-txt-file-output-end-with-system-int32-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!