将项添加到列表而不包含列表实例

时间:2015-03-10 06:20:54

标签: c# list

   PraseItems(Ilist<int> items)
    {
    ....
    }

为什么以下语法抛出错误但其他语法不是

   ParseItems(new List<int>().Add(5)); -- throws Invalid arguments error

   ParseItems(new List<int>(5)); -- No error

1 个答案:

答案 0 :(得分:4)

正确的语法是

  new List<int>() { 5 };

在您的样本中

  List<int>(5)

你只是为5个项目分配内存