Adding elements to a C# array

前端 未结 10 421
野趣味
野趣味 2021-01-11 19:29

I would like to programmatically add or remove some elements to a string array in C#, but still keeping the items I had before, a bit like the VB function ReDim Preserve.

10条回答
  •  一向
    一向 (楼主)
    2021-01-11 19:55

    One liner:

        string[] items = new string[] { "a", "b" };
    
        // this adds "c" to the string array:
        items = new List(items) { "c" }.ToArray();      
    

提交回复
热议问题