Why can I initialize a List like an array in C#?

前端 未结 6 1013
清歌不尽
清歌不尽 2020-12-12 23:20

Today I was surprised to find that in C# I can do:

List a = new List { 1, 2, 3 };

Why can I do this? What constructor

6条回答
  •  情深已故
    2020-12-12 23:38

    It works thanks to collection initializers which basically require the collection to implement an Add method and that will do the work for you.

提交回复
热议问题