C # operator overload with <T> list

I am trying to overload a statement in C # (don't ask why!) Which applies to Lists . For example, I would like to write:

 List<string> x = // some list of things List<string> y = // some list of things List<string> z = x + y 

so that 'z' contains all the contents of 'x', followed by the contents of 'y'. I know that there are already ways to combine the two lists, I'm just trying to understand how operator overloading works with common structures.

(By the way, this is the List class from Systems.Collections.Generic ).

+6
source share
1 answer

As far as I know, this is not feasible: you must implement operator overloading in the type that uses it. Since List<T> not your type, you cannot override operators in it.

However, you can get your own type from the List<string> and override the statement inside your class.

 class StringList : List<string> { public static StringList operator +(StringList lhs, StringList rhs) { } } 
+7
source

Source: https://habr.com/ru/post/907655/


All Articles