Click here to Skip to main content
15,881,600 members
Please Sign up or sign in to vote.
3.00/5 (4 votes)
See more:
Hi All,

What is wrong with this bit of code?

C#
public static List<int> operator +(List<int> a, List<int> b)
        {
            if (!(a.Count == b.Count))
            {
                return a;
            }
            else
            {
                return b;
            }


I want to be able to add all values from one List<int> to another, but I get this error: One of the parameters of a binary operator must be the containing type.

I cannot see whats wrong. According to what I understand, at least one of my parameters IS the containing type.

Thanks in advance.
Posted

First, your code doesn't add all of the elements in one list to another list. It merely returns ONE of the specified lists.

Second, why don't you use the List.AddRange method instead (if you really want to combine the lists)?

C#
List<string> list1 =  new List<string>();
List<string> list2 =  new List<string>();

// ... add items to both lists

list1.AddRange(list2.ToArray());


Now, if you're trying to add the actual values in one list to the values in the other list, I would write an extension method instead:

C#
public static void AddIntValues(this List<int> me, List<int> other)
{
    int max = Math.Min(me.Count, other.Count);
    for (int i = 0; i < max; i++)
    {
        me[i] += other[i];
    }
}


This is, admittedly, a pretty bizarre requirement.
 
Share this answer
 
v3
Comments
Sergey Alexandrovich Kryukov 24-Mar-11 13:25pm    
Good point, a 5.
--SA
PJ du Preez 25-Mar-11 1:59am    
"First, your code doesn't add all of the elements in one list to another list. It merely returns ONE of the specified lists."

I know, it was to keep the code simple for the question. What is being returned is negligible.

"Second, why don't you use the List.AddRange method instead (if you really want to combine the lists)?"

This is not what I want to do, if it was, I'd use List.AddRange. I want to at List1[i] to List2[i], hence the need to overload the operator (unless there is a simpler way to add corrosponding elements together)

Using an extention method is not as intuitive as simply saying List3 = List1 + List2, if you see what I mean.

Thanks for your view, though!
#realJSOP 25-Mar-11 5:50am    
With an extension method, you can make the parameter types generic, and use it for *any* numeric or string type without writing additional code, but whatever.
To overload an operator you need to have at least one instance of the containing class as a parameter. i.e if you had a class Customer then you would overload the + operator like this

C#
public static Customer operator +(Customer a, Customer b)
            {
                //Do stuff here
            }

The way you are trying to do it is if you are trying to overload the binary operator for List<int> which is not possible.

If I were you, I would have a class , using the Customer analogy that inherits from List<customer> and then over load the binary operator for that class, something like this

C#
class Customer
        {
        }
        class Customers:List<Customer>
        {
            public static Customers operator +(Customers a, Customers b)
            {
                //Do stuff here
            }
        }

Hope this helps
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 24-Mar-11 13:25pm    
That is good, too. My 5.
--SA
Do you mean this:
C#
class MyList : List<int>
{
    public static MyList operator +(MyList a, MyList b)
    {
        if (!(a.Count == b.Count))
        {
            return a;
        }
        else
        {
            return b;
        }
    }
}


?
 
Share this answer
 
Comments
Wayne Gaylard 24-Mar-11 7:50am    
Who downvoted this. It is the same answer as mine. Have 5
CPallini 24-Mar-11 8:01am    
Thanks. :-)
Sergey Alexandrovich Kryukov 24-Mar-11 13:23pm    
Who, who... one of the morons, of course.
I vote 5, too.
--SA
PJ du Preez 25-Mar-11 1:52am    
@CPallini

Hi, thanks. Yes this is what I was wanting to do. I did operator overloading in C++, very similar in C#, couldn't understand where I was going wrong.

Thanks!
PJ du Preez 25-Mar-11 2:18am    
Ah, I see where I went wrong with this... In C++ I created a List<t> class from scratch, and added the overloaded + operator to THAT class. .Net already have a List<t> Class and thus I need to create a new inherited class to override the operator.

Thanks!

And the clouds parted, the sun shined through. Now I understand.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900