In C#, is there a way to write custom object initializers for new data-types?

前端 未结 3 1373
孤独总比滥情好
孤独总比滥情好 2021-02-09 08:43

In C#, there\'s the \"standard\" initializer technique { Property1 = \"a\", Property2 = \"b\" }, and there are a couple of special variants for collections (list and dictionary)

3条回答
  •  心在旅途
    2021-02-09 09:24

    Using a variadic local lambda n that simply calls your constructor, you could get it as short as:

    n(item1, n(item2, item3, item4), n(item5, item6))
    

    Update: something like

    var n = (params Node[] nodes) => new Node(nodes);
    

提交回复
热议问题