Auto-initializing C# lists

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

    For more complex types:

    List listOfCustomers =
            new List {
                { Id = 1, Name="Dave", City="Sarasota" },
                { Id = 2, Name="John", City="Tampa" },
                { Id = 3, Name="Abe", City="Miami" }
            };
    

    from here: David Hayden's Blog

提交回复
热议问题