Is there a more elegant solution for adding an item to an IEnumerable than this
myNewIEnumerable = myIEnumerable.Concat ( Enumerable.Repeat (item, 1) );
?
Some context:
public void printStrings (IEnumerable<string> myStrings) {
Console.WriteLine ("The beginning.");
foreach (var s in myStrings) Console.WriteLine (s);
Console.WriteLine ("The end.");
}
...
var result = someMethodDeliveringAnIEnumerableOfStrings ();
printStrings (result.Concat ( Enumerable.Repeat ("finished", 1) ) );
Sure, there is. Concat() takes any IEnumerable<T>
, including arrays:
var myNewIEnumerable = myIEnumerable.Concat(new[] { item });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With