Clean way of adding elements to IEnumerable

CChristoffer10/5/2022
Is there a good way of adding an element to an IEnumerable without having to create a copy of the collection as such?:

var CatA = new Cat();
var CatB = new Cat();
IEnumerable<Cat> cats = new[]{ CatA };
var catList = cats.ToList();
catList.Add(CatB));
cats = catList;

What I want to do is something like this:

var CatA = new Cat();
var CatB = new Cat();
IEnumerable<Cat> cats = new[]{ CatA };
cats.ToList().Add(CatB));

But ToList() makes a copy of the IEnumerable cats, and CatB is placed into the copy, instead of the IEnumerable cats.
AAntonC10/5/2022
Use the Concat Linq method
CChristoffer10/5/2022
Hah, speaking of Cats. Thanks. I'll try it out
OOrannis10/5/2022
Why are you not just starting with a List?
OOrannis10/5/2022
And to be clear, Concat isn't going to do what you want either
OOrannis10/5/2022
Concat produces a new enumerable, which returns the contents of the first enumerable, then the contents of the added elements
OOrannis10/5/2022
You cannot add elements to an IEnumerable
AAntonC10/5/2022
yeah, but note that it does not copy the collection
AAntonC10/5/2022
it only makes a new iterator object
HHin10/5/2022
The purpose of IEnumerable is just to assure iterability, if you want to be able to add thing then IList
HHin10/5/2022
Wait, array also implements IList, donnit?
HHin10/5/2022
iirc Array is IList but throws exceptions upon adding so am unsure
OOrannis10/5/2022
Yes