I have implemented a class, which combines several IEnumerable objects to a single IEnumerable object, so I can write:
foreach (SomeType item in new CombinedEnumerable<SomeType>(it1, it2))
{
...
}
Where it1 and it2 implement the IEnumerable< SomeType > interface. For esthetic reasons and for code clarity I would like to be able to write:
foreach (SomeType item in it1 + it2)
{
...
}
All I would need to do is implement this operator:
public static IEnumerable<T> operator +(IEnumerable<T> t1, IEnumerable<T> t2)
{
return new CombinedEnumerable<T>(t1, t2);
}
But the compiler won't let me do this, because the definition would have to be inside the class IEnumerable< T >. Is there a workaround?
Edit: the possible duplicate "How to concatenate two IEnumerable into a new IEnumerable?" did not solve the problem. This part I already had solved. What I did ask for was how to implement the "+" operator (maybe as an extension method), which seems not to be possible.