C# System.String[] 返回而不是数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12146723/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 22:16:23  来源:igfitidea点击:

System.String[] returned instead of array

c#

提问by Peter Van Akelyen

I'm trying to return an array of strings but return test;only gives "System.String[]" as output. If I iterate through test[] all values are filled in correctly. What am I doing wrong?

我试图返回一个字符串数组,但return test;只给出“System.String[]”作为输出。如果我遍历 test[] 所有值都正确填写。我究竟做错了什么?

 public static String[] ReturnTheStrings()
 {
     FillTheArray();
     String[] test = new String[178];

     for (int i = 0; i < 178; i++)
     {
         if ((Class7.Method4(strings[i])) == "decoding wrong")
         {
             test[i] = strings[i+1];
             //System.Console.WriteLine("Non-encoded value");
         }
         else
         {
             test[i] = Class7.Method4(strings[i+1]);
             //System.Console.WriteLine("Encoded value");
         }
     }
     return test;
 }

I'm using MS Visual C# 2010.

我正在使用 MS Visual C# 2010。

采纳答案by jrummell

Calling .ToString()on an array will return "System.String[]". If you want to display each value in the array, you have iterate over it.

调用.ToString()数组将返回“System.String[]”。如果要显示数组中的每个值,则需要对其进行迭代。

For example:

例如:

foreach (var value in test)
{
    Console.WriteLine(value);
}

Or, as @Oded pointed out, you can use String.Join.

或者,正如@Oded 指出的那样,您可以使用String.Join

Console.WriteLine(String.Join(Environment.NewLine, stringArray));