Auto-initializing C# lists

后端 未结 6 2131
有刺的猬
有刺的猬 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 05:58

    A bit late, but maybe still of interest: Using LINQ, try

    var initializedList = new double[10].ToList()

    ...hopefully avoiding copying the list (that's up to LINQ now).

    This should be a comment to Michael Meadows' answer, but I'm lacking reputation.

提交回复
热议问题