如何检查 IEnumerable 是否为列表?

Mat*_*hew 2 .net ienumerable types list

给定一个 IEnumerable,如何检查它的类型是否为 List?

给定一个 IEnumerable,我想执行 List 方法,但如果它已经是一个 List,我将简单地转换它而不是使用 .ToList()

Ode*_*ded 5

使用is运算符来测试变量的类型。

if(myIEnumerable is IList)
{
   // it is a List (may still need casting in order to use List specific methods)
}
Run Code Online (Sandbox Code Playgroud)