Here\'s how I would add one item to an IEnumerable object:
//Some IEnumerable object
IEnumerable arr = new string[] { \"ABC\", \"DEF\"
Write an extension method ConcatSingle
:)
public static IEnumerable ConcatSingle(this IEnumerable source, T item)
{
return source.Concat(new [] { item } );
}
But you need to be more careful with your terminology.
You can't add an item to an IEnumerable
. Concat
creates a new instance.
Example:
var items = Enumerable.Range(1, 10)
Console.WriteLine(items.Count()); // 10
var original= items;
items = items.ConcatSingle(11);
Console.WriteLine(original.Count()); // 10
Console.WriteLine(items.Count()); // 11
As you can see, the original enumeration - which we saved in original
didn't change.