【问题标题】:Newbie: C# List returns back System.Collections.Generic.List新手:C# List 返回 System.Collections.Generic.List
【发布时间】:2013-12-10 16:28:24
【问题描述】:

看起来这很简单:

class Program
{
    static void Main()
    {
        List<string> lst = new List<string>();
        lst.Add("String 1");
        lst.Add("String 2");

        foreach (string item in lst)
        {
            Console.WriteLine(lst);
        }
        Console.ReadLine();
    }
}

然而它返回:

System.Collections.Generic.List`1[System.String]

【问题讨论】:

  • 您正在编写 lst 对象而不是 item 对象
  • 呃!!!我不敢相信我犯了那个错误。现在说得通了。
  • 别担心。发生在我们最好的人身上:)

标签: c# list collections foreach


【解决方案1】:

或者你可以使用这个:

Console.WriteLine(string.Join("\n", lst));

【讨论】:

    【解决方案2】:

    试试这个:

    foreach (string item in lst)
    {
        Console.WriteLine(item);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-01-04
      • 2020-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-27
      • 1970-01-01
      相关资源
      最近更新 更多