What's the Best Way to Add One Item to an IEnumerable?

前端 未结 6 1511
余生分开走
余生分开走 2021-01-01 09:43

Here\'s how I would add one item to an IEnumerable object:

//Some IEnumerable object
IEnumerable arr = new string[] { \"ABC\", \"DEF\"         


        
6条回答
  •  北海茫月
    2021-01-01 10:30

    IEnumerable is immutable collection, it means you cannot add, or remove item. Instead, you have to create a new collection for this, simply to convert to list to add:

    var newCollection = arr.ToList();
    newCollection.Add("JKL"); //is your new collection with the item added
    

提交回复
热议问题