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

前端 未结 6 1520
余生分开走
余生分开走 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:16

    Append() - is exactly what you need, it has been added to the .NET Standard (in 2017), so you no longer need to write your own extension methods. You can simply do this:

    arr = arr.Append("JKL");

    Since .NET is open source, here you can look on the implementation (it is more sophisticated than custom methods suggested above): https://github.com/dotnet/runtime/blob/master/src/libraries/System.Linq/src/System/Linq/AppendPrepend.cs

提交回复
热议问题