0

I tried to code something like this:

public static List<T> operator+<T>(List<T> list1, List<T> list2)
{
    return list1.Concat(list2).ToList();
}

It didn't work.

None of the questions I could find about "operator overloading with generics" were about extension methods. This question is not a duplicate because the other question doesn't mention generic types.

12
  • 1
    Essentially the same question as stackoverflow.com/questions/5066281/…, but for a different operator. I do not believe that changes the answer. Commented Dec 22, 2017 at 16:27
  • If you search for "c# operator extension method" you should see it is simply not possible Commented Dec 22, 2017 at 16:50
  • @hvd That question has nothing to do with generic types. Commented Dec 22, 2017 at 16:59
  • 2
    Please do not do this. Its bad enough that strings and delegates overload + Commented Dec 22, 2017 at 17:06
  • 1
    Now, if you are in fact hell bent on doing this crazy thing, then what you have to do is create your own type, create an addition operator on that type, and then create a conversion operator on that type from lists to your type. But again, please do not. If you wish to express the concatenation of two sequences to form a third, we already have that, as you know: seq1.Concat(seq2) is nicely concise. It's already the right thing to do. Don't mess with it. Commented Jan 2, 2018 at 21:41

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.