Auto-initializing C# lists

后端 未结 6 2133
有刺的猬
有刺的猬 2020-12-14 05:15

I am creating a new C# List (List). Is there a way, other than to do a loop over the list, to initialize all the starting values to 0?

6条回答
  •  伪装坚强ぢ
    2020-12-14 06:03

    One possibility is to use Enumerable.Range:

    int capacity;
    var list = Enumerable.Range(0, capacity).Select(i => 0d).ToList();
    

    Another is:

    int capacity;
    var list = new List(new double[capacity]);
    

提交回复
热议问题