Auto-initializing C# lists

后端 未结 6 2142
有刺的猬
有刺的猬 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:15

    In addition to the functional solutions provided (using the static methods on the Enumerable class), you can pass an array of doubles in the constructor.

    var tenDoubles = new List(new double[10]);
    

    This works because the default value of an double is already 0, and probably performs slightly better.

提交回复
热议问题