2012-11-22 26 views
2

好,我是在得到另一个SO问题的答案的过程中,我想出了下面的函数来获取廉政局的一个独特的列表:为什么是变种的Int32本例中没有列出<Int32>

static List<Int32> Example(params List<Int32> lsts) 
    { 
     List<Int32> result = new List<int>(); 

     foreach (var lst in lsts) 
     { 
      result = result.Concat(lst).ToList(); 
     } 

     return result.Distinct().OrderBy(c => c).ToList(); 
    } 

当我在VS2012中查看var时,它说它的类型为Int32而不是List<Int32>。如下图所示:

The problem

不宜VAR是List<Int32>类型?

回答

9

你在参数类型声明的末尾缺少一个[]

//           v-- this is missing in your code 
static List<Int32> Example(params List<Int32>[] lsts) 
{ 
    List<Int32> result = new List<int>(); 

    foreach (var lst in lsts) 
    { 
     result = result.Concat(lst).ToList(); 
    } 

    return result.Distinct().OrderBy(c => c).ToList(); 
} 
+0

宾果,谢谢:)以为这会是简单的事情。 –

5

你被一个不同的编译器错误误导了。
您的参数不是数组。

您需要将参数更改为params List<Int32>[] lsts以使其成为一个列表数组。 (或更好,但params IEnumerable<Int32>[] lsts

请注意,你也可以完全摆脱了foreach环和写入

return lsts.SelectMany(list => list) 
      .Distinct() 
      .OrderBy(i => i) 
      .ToList(); 
+0

现在明白了,谢谢:) –

0

带有关键字params的参数必须是数组。

相关问题