-2

C# operator overloading with List<T>

Why not inherit from List<T>?

Based upon the first question I have implemented operator overloading to one of my List by inheriting from it.

Today I stumbled upon the second link. So my question is "Is it possible to do operator overloading with List without inheriting from it?"

//Copying the content of first question

I'm trying to overload an operator in C# (don't ask why!) that applies to Lists. For example, I'd like to be able 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'm aware that there are already ways to combine two lists, I'm just trying to understand how operator overloading works with generic structures.

(This is the List class from Systems.Collections.Generic, by the way.)

Deepak Mishra
  • 2,984
  • 1
  • 26
  • 32
  • 1
    Why not writing an extension method instead of trying to do an operator overload? On the plus side, the extension method could be general purpose by working on any type implementing `IList` (or `ICollection`; whatever you want/need...) –  Dec 27 '18 at 11:57
  • @elgonzo: can you write that for the first question? I don't want that functionality in other lists. – Deepak Mishra Dec 27 '18 at 11:57
  • 1
    The example which explains why not to inherit recommends composition over inheritance. operation overloading is not composition. its just an extra functionality you want to add to existing class. So you are not wrong if you use inheritance for operator overloading. You can have some better approach for that such as extension method or having an interface at the base etc. – Chetan Dec 27 '18 at 11:58
  • @ChetanRanpariya Extension method would add that functionality to all lists, I want for some lists only. – Deepak Mishra Dec 27 '18 at 12:00
  • @DeepakMishra, incorrect. Extension methods are not required to work on all (list) types. An extension method works on such types that are assignable to the type of the `this` parameter of the extension method. (And can even be further constricted by using a generic type constraint if the extension method is generic) –  Dec 27 '18 at 12:01
  • @All: I am looking for an answer, not comments. – Deepak Mishra Dec 27 '18 at 12:01
  • @elgonzo how? Could you please explain by example? Could you please write an answer for first question with your approach of extension methods? – Deepak Mishra Dec 27 '18 at 12:05
  • There is no authorative, definitive answer to your question "_What to do?_". It depends very much on your particular, individual application scenario. A possible answer to the question could fundamentally change with a changing application scenario. There is no single answer to this question, hence why somewhat vague responses to a somewhat vague question are in the comments and not in an "vague" answer. Just my opinion of course. –  Dec 27 '18 at 12:08
  • @elgozo: Ok I will remove that, now my question is "Is it possible to do operator overloading with List without inheriting from it?" – Deepak Mishra Dec 27 '18 at 12:09
  • I guess the answer would be either "Yes" or "No" with some reasons of the same. – Deepak Mishra Dec 27 '18 at 12:12
  • FYI with respect to extension methods: If you are unfamiliar with extension methods, perhaps this might be helpful: https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/extension-methods –  Dec 27 '18 at 12:15
  • @elgonzo https://stackoverflow.com/questions/172658/operator-overloading-with-c-sharp-extension-methods – Deepak Mishra Dec 27 '18 at 12:18
  • Why do you show me that question? I was not suggesting to make some "extension operator" (which of course is not possible). I was suggesting implementing/using extension methods. My apologies if that was misunderstood... –  Dec 27 '18 at 12:18
  • @elgonzo okay, so you thought that I don't know about extension methods. Thank you for your thoughts. – Deepak Mishra Dec 27 '18 at 12:22
  • Yes, i though that you might not have much experience writing extension methods because you asked to explain my suggestion about extension methods "by example" and about "my approach of extension methods". If you take offence of me thinking that based on your previous comment, well, i guess i better leave you to your business then... –  Dec 27 '18 at 12:25
  • @elgonzo No offence at all, I just wanted you to write the answer. – Deepak Mishra Dec 27 '18 at 12:27
  • Ah, okay; second misunderstanding in a row ;-) Me throwing out the suggestion about using extension methods was about the "What to do?" part of your question, and i already explained why i (personally) am hesitant to write an answer. The second part of your question with regard to whether it is possible to overload operators for existing types "from the outside" is answered by another question here on SO already (and thus has already been flagged by Stijn - as well as me - as a duplicate question of https://stackoverflow.com/questions/618149/operator-overloading-for-builtin-types) –  Dec 27 '18 at 12:33
  • @elgonzo I was just looking for the confirmation of the point that for operator overloading, it is okay to inherit from List, but it seems that nobody was able to say that. It helps in making design decisions if we get such confirmations. – Deepak Mishra Dec 27 '18 at 12:44
  • Of course it is okay to inherit from `List`. After all, the type is not sealed, so you are allowed to inherit from it. Even the answer by none other than Eric Lippert in the question you linked to (https://stackoverflow.com/questions/21692193/why-not-inherit-from-listt) says that it is acceptable to derive from List. (That doesn't imply or suggest an ability to customize an operator for non-inherited List instances, of course) Now, whether there would be any drawbacks specific with regard to your specific application scenario can be answered by nobody else than you, of course... –  Dec 27 '18 at 12:53

1 Answers1

1

you can not do operator overload outside the class. you can use container for operator overloading

you can run this code hereurl


using System;
 using System.Collections;
 using System.Collections.Generic;

  public class Program
  {
    public static void Main()
    {
        Run();
    }

    public static void Run()
         {
             var ls = new List<int>(){
                 1,3,5,7,9,11
             };
             var ls2 = new List<int>(){
                 2,4,6,8,10
             };

             var lsContainer = new AdditionContainer<List<int>,int>(ls);
             var ls2Container = new AdditionContainer<List<int>,int>(ls2); 
             var finalAnsower =   lsContainer +ls2Container;
             finalAnsower.List.ForEach(f=>Console.WriteLine(f) );


    }


}


public class AdditionContainer<TList,T>

     where TList:List<T>
{
         public TList List;
         public AdditionContainer(TList list )
         {
               List=list;   
         }

         public static AdditionContainer<TList,T> operator +(AdditionContainer<TList,T> a, AdditionContainer<TList,T> b)
         {
               b.List.ForEach(l =>{
                  if(!a.List.Contains(l))
                  {
                     a.List.Add(l);  
                  }
               });
               return a;
         }       
     }
divyang4481
  • 1,584
  • 16
  • 32