Add to a readonly collection in a constructor?

后端 未结 4 1888
醉梦人生
醉梦人生 2021-01-24 05:04

Is there a c# language construct that will allow me to add items to a readonly collection property in a constructor? I want to do something like this:

public cl         


        
4条回答
  •  孤街浪徒
    2021-01-24 05:39

    Try this. It is definitely possible to add elements on construction

    var node = new Node
     {
         Children =
        {
            new Node(),
            new Node()
        }
     };
    

提交回复
热议问题