Shortcut for creating single item list in C#

前端 未结 13 1857
迷失自我
迷失自我 2020-12-14 05:06

In C#, is there an inline shortcut to instantiate a List with only one item.

I\'m currently doing:

new List( new string[] { \"         


        
13条回答
  •  醉酒成梦
    2020-12-14 05:43

    new[] { "item" }.ToList();
    

    It's shorter than

    new List { "item" };
    

    and you don't have to specify the type.

提交回复
热议问题