My question as title above. For example,
IEnumerable<T> items = new T[]{new T("msg")};
items.ToList().Add(new T("msg2"));
but after all it only has 1 item inside.
Can we have a method like items.Add(item)
?
like the List<T>
My question as title above. For example,
IEnumerable<T> items = new T[]{new T("msg")};
items.ToList().Add(new T("msg2"));
but after all it only has 1 item inside.
Can we have a method like items.Add(item)
?
like the List<T>
You cannot, because
IEnumerable<T>
does not necessarily represent a collection to which items can be added. In fact, it does not necessarily represent a collection at all! For example:What you can do, however, is create a new
IEnumerable
object (of unspecified type), which, when enumerated, will provide all items of the old one, plus some of your own. You useEnumerable.Concat
for that:This will not change the array object (you cannot insert items into to arrays, anyway). But it will create a new object that will list all items in the array, and then "Foo". Furthermore, that new object will keep track of changes in the array (i.e. whenever you enumerate it, you'll see the current values of items).
A couple short, sweet extension methods on
IEnumerable
andIEnumerable<T>
do it for me:Elegant (well, except for the non-generic versions). Too bad these methods are not in the BCL.
Not only can you not add items like you state, but if you add an item to a
List<T>
(or pretty much any other non-read only collection) that you have an existing enumerator for, the enumerator is invalidated (throwsInvalidOperationException
from then on).If you are aggregating results from some type of data query, you can use the
Concat
extension method:Edit: I originally used the
Union
extension in the example, which is not really correct. My application uses it extensively to make sure overlapping queries don't duplicate results.No the IEnumerable doesn't support adding items to it.
You 'alternative' is:
I just come here to say that, aside from
Enumerable.Concat
extension method, there seems to be another method namedEnumerable.Append
in .NET Core 1.1.1. The latter allows you to concatenate a single item to an existing sequence. So Aamol's answer can also be written asStill, please note that this function will not change the input sequence, it just return a wrapper that put the given sequence and the appended item together.
The type
IEnumerable<T>
does not support such operations. The purpose of theIEnumerable<T>
interface is to allow a consumer to view the contents of a collection. Not to modify the values.When you do operations like .ToList().Add() you are creating a new
List<T>
and adding a value to that list. It has no connection to the original list.What you can do is use the Add extension method to create a new
IEnumerable<T>
with the added value.Even in this case it won't modify the original
IEnumerable<T>
object. This can be verified by holding a reference to it. For exampleAfter this set of operations the variable temp will still only reference an enumerable with a single element "foo" in the set of values while items will reference a different enumerable with values "foo" and "bar".
EDIT
I contstantly forget that Add is not a typical extension method on
IEnumerable<T>
because it's one of the first ones that I end up defining. Here it is