我List<int>通过存储两个简单常量MinValue和MaxValue来初始化构造函数:
private const int MinValue = 1;
private const int MaxValue = 100;
private List<int> integerList = new List<int>();
public Class()
{
for (int i = MinValue ; i < MaxValue ; i++)
{
integerList .Add(i);
}
}
Run Code Online (Sandbox Code Playgroud)
有没有办法用简单的LINQ查询初始化列表?由于a List<T>可以用a构造IEnumerable<T>,是否存在以下形式的查询?
private List<int> integerList = new List<int>(<insert query here>);
Run Code Online (Sandbox Code Playgroud)
这甚至可能吗?
Tim*_*oyd 10
这可以通过使用来实现 Enumerable.Range
List<int> integerList = Enumerable.Range(MinValue, MaxValue - MinValue).ToList();
Run Code Online (Sandbox Code Playgroud)