C#我得到System.Object []而不是数组中的值

时间:2014-05-02 23:30:05

标签: c# arrays

关于数组的另一个问题。当我创建一个数组并从键盘填充它时,控制台显示几个System.Object []而不是我设置的值。例如,如果我创建一个[5]数组,我得到36 System.Object []而不是我的5个值。这是为什么?这是我正在使用的代码:

 object[] row = new object[5];

 public void fill()
    {
        for (int i = 0; i < 5; i++)
            {
                Console.WriteLine("Set the values" + (i+1));
                row[i] = Console.ReadLine();
            }
        Console.ReadKey();
    }

 public void view()
    {
        Console.WriteLine("The values are:");
        for (int i = 0; i <= 5; i++)
        {
            Console.Write("\n");
            for (int j = 0; j <= 5; j++)
            {
                Console.Write(row);
            }
        }
        Console.ReadKey();
    }

    static void Main(string[] args)
    {
        Program objeto = new Program();

        objeto.fill();
        objeto.view();

        Console.ReadKey();
    }

没有错误消息,但在屏幕上我得到这个设置5个值:

值为:

  

System.Object的[] System.Object的[] System.Object的[] System.Object的[] System.Object的[] System.Object的[]   System.Object的[] System.Object的[] System.Object的[] System.Object的[] System.Object的[] System.Object的[]   System.Object的[] System.Object的[] System.Object的[] System.Object的[] System.Object的[] System.Object的[]   System.Object的[] System.Object的[] System.Object的[] System.Object的[] System.Object的[] System.Object的[]   System.Object的[] System.Object的[] System.Object的[] System.Object的[] System.Object的[] System.Object的[]   System.Object的[] System.Object的[] System.Object的[] System.Object的[] System.Object的[] System.Object的[]

我该怎么办?

3 个答案:

答案 0 :(得分:6)

我认为它应该是row[j]

for (int j = 0; j <= 5; j++)
{
    Console.Write(row[j].ToString());
}

答案 1 :(得分:4)

由于row是一个数组,因此默认的.ToString()方法正在打印System.Object[]类型。

尝试将row[j]的值改为row的值。

除此之外,您似乎有两个循环,几乎看起来您想要打印多维数组,而您的数组则不是。所以外部for循环是不必要的,除非你想要打印5行相同的数据。

答案 2 :(得分:0)

将您的观点更改为此...

public void view()
    {
        Console.WriteLine("The values are:");
        for (int i = 0; i <= 4; i++)
        {
            Console.Write("\n");
            Console.Write(row[i]);
        }
        Console.ReadKey();
    }
相关问题