0

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.

Gerhard
  • 1,342
  • 2
  • 12
  • 23
  • You could have it1 and it2 be of a type that has overloaded the + operator, returning your CombinedEnumerable class. Another option would be to create an extension method that constructs the CombinedEnumerable class, then your syntax could be `foreach (SomeType item in it1.Plus(it2)) { ...`. Your extenstion method would look something like this: `public static IEnumerable Plus(this IEnumerable current, IEnumerable other) => new CombinedEnumerable(current, other);` – James Cockayne Sep 20 '18 at 11:28
  • 1
    Why not use [`concat`](https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.concat?view=netframework-4.7.2#System_Linq_Enumerable_Concat__1_System_Collections_Generic_IEnumerable___0__System_Collections_Generic_IEnumerable___0__)? that would look like `foreach(var item in it1.Concat(it2)))`... – Zohar Peled Sep 20 '18 at 11:28
  • Possible duplicate of [How to concatenate two IEnumerable into a new IEnumerable?](https://stackoverflow.com/questions/14164974/how-to-concatenate-two-ienumerablet-into-a-new-ienumerablet) – Aleks Andreev Sep 20 '18 at 11:29
  • Who knows what CombinedEnumerable does @ZoharPeled ! Could be different to Concat, Zip etc – James Cockayne Sep 20 '18 at 11:31
  • 1
    @AleksAndreev - no the OP knows how to do that. This is about op overloading. – H H Sep 20 '18 at 11:36
  • @james Cockayne and Zohar Peled: CombinedEnumerable does not create a new generic collection (no concat), but iterates through the first and then the second collection, no new allocation. it1, it2 may be any generic collection, so I would have to derive all of them. – Gerhard Sep 20 '18 at 11:41
  • 3
    @Gerhard that's what Concat would do. It won't copy the collections, it simply keeps a reference to them. Check out the source code for hours of entertainment! [Concat source code](https://referencesource.microsoft.com/#System.Core/System/Linq/Enumerable.cs,9c0a66e386e37265) – James Cockayne Sep 20 '18 at 11:42
  • @James. thank you, I didn't know that. This would have spared me the implementation of CombinedEnumerable (which was not much) – Gerhard Sep 20 '18 at 11:47
  • @JamesCockayne Correct, it was mostly an educated guess, but apparently a good one :-). If I would have known for sure, I would just close as a dupe – Zohar Peled Sep 20 '18 at 11:50
  • 1
    @Gerhard: _"for code clarity"_; i strongly doubt that this would make your code clearer, quite the opposite. Every developer would wonder what the heck is going on there. They would ask themselves, is that an addition, why he can use it in a `foreach`? – Tim Schmelter Sep 20 '18 at 11:54
  • 1
    If you've defined the two original collection types then add an operator to one or both, that's the only way to do this. You can't add operators from outside of the types involved. – Lasse V. Karlsen Sep 21 '18 at 06:47

0 Answers0