42

What exactly (under the hood) do the += and -= operators do?

Or are they implicit in that they are defined per type?

I've used them extensively, it's a very simple feature of the syntax, but I've never thought about the depths of how it works.

What Brought About the Question

I can concatenate a string value like so:

var myString = "hello ";
myString += "world";

All fine. But why doesn't this work with collections?

var myCol = new List<string>();
myCol += "hi";

You may say 'well you're attempting to append a different type, you can't append a string to a type that isn't string'. But the following doesn't work either:

var myCol = new List<string>();
myCol += new List<string>() { "hi" };

Ok, maybe it doesn't work with collections, but is the following not a (kind of) collection of event handlers?

myButton.Click += myButton_Click;

I'm obviously lacking an in-depth understanding of how these operators work.

Please note: I'm not trying to build the collection myCol in this way, in a real project. I'm merely curious about the workings of this operator, it's hypothetical.

7
  • 6
    It may be worth noting that "what is addition" is actually quite a tricky little concept in mathematics. It takes a lot of very carefully constructed work to define it, even though the concept is intuitive to many. A similar corollary arises in computer language design. While defining what += does is easy, the in depth understand of why we wrote it to work that way is a bit more time consuming.
    – Cort Ammon
    Nov 9, 2016 at 14:58
  • I mean, really, what should happen with "10" + "20" - "1020"? Or "30"? Who is to tell someone that there's a right or wrong way to answer that?
    – corsiKa
    Nov 9, 2016 at 17:05
  • 2
    The fundamental problem here is that + is used to mean normal numeric addition, string concatenation, and sequencing of multicast delegates, and all three of these operations are only tangentially related to each other. It's a bit of an abuse of the intuition we all have about addition, and this leads to confusion. Your best bet is to think of + and += as being several different things that have the same syntax for historical reasons. Nov 9, 2016 at 19:32
  • 1
    @corsiKa "10" + "20" is "1020" as they are strings. 10 + 20 is 30 as they are numbers. isn't that obvious (in a staticly typed language)?
    – Sharky
    Nov 10, 2016 at 7:59

4 Answers 4

41

The += operator is implicitly defined like this: a += b turns into a = a + b;, same with the -= operator.
(caveat: as Jeppe pointed out, if a is an expression, it is only evaluated once if you use a+=b, but twice with a=a+b)

You cannot overload the += and -= operator separately. Any type that supports the + operator also supports +=. You can add support for += and -=to your own types by overloading + and -.

There is however one exception hard coded into c#, which you have discovered:
Events have a += and -= operator, which adds and removes an event handler to the list of subscribed event handlers. Despite this, they do not support the + and - operators.
This is not something you can do for your own classes with regular operator overloading.

13
  • 21
    The usual thing to be aware of, is that the left-hand side of += is evaluated only once. For that reason CallMethod().Member += 10; is not quite the same as CallMethod().Member = CallMethod().Member + 10; because with the former the CallMethod is invoked only once, whereas with the latter it is invoked twice. Nov 9, 2016 at 14:19
  • 1
    @SamC because the adding/combining (+) happens first, before the assignment (=), hence the order of +=
    – kwah
    Nov 9, 2016 at 15:29
  • 3
    @SamC I think it would also be ambiguous between the =+ operator and using the assignment operator followed by the unary + operator. How would the compiler know if "a =+ b" means "a = a + b" or "a = (+b)"?
    – George T
    Nov 9, 2016 at 15:46
  • 1
    @GeorgeT That ambiguity would be resolved by maximal munch, which is almost certainly the general rule in C# (languages that don't do maximal-munch tokenization are very unusual, and I can't think of one off the top of my head). This is purely historical - see reply to SamC below.
    – zwol
    Nov 9, 2016 at 16:40
  • 1
    @SamC Historical reasons. In the very first iterations of the language now known as C, the shorthand was a =+ b, and the same for all the other op-and-assign operators. They were changed to += between Unix V6 and Unix V7 for readability reasons: given a=-b, did the programmer mean to write a =- b or a = -b? The V6 compiler would take the former interpretation, but Kernighan and Ritchie evidently felt that the second was likely to have been meant. C# borrowed all this from C verbatim.
    – zwol
    Nov 9, 2016 at 16:46
28

As the other answer say, the + operator is not defined for List<>. You can check it trying to overload it, the compiler would throw this error One of the parameters of a binary operator must be the containing type.

But as an experiment, you can define your own class inheriting List<string> and define the + operator. Something like this:

class StringList : List<string>
{
    public static StringList operator +(StringList lhs, StringList rhs)
    {
        lhs.AddRange(rhs.ToArray<string>());
        return lhs;
    }
}

Then you can do this without problems:

StringList listString = new StringList() { "a", "b", "c" };
StringList listString2 = new StringList() { "d", "e", "f" };
listString += listString2;

Edit

Per @EugeneRyabtsev comment, my implementation of the + operator would lead to an unexpected behavior. So it should be more something like this:

public static StringList operator +(StringList lhs, StringList rhs)
{
      StringList newList=new StringList();
      newList.AddRange(lhs.ToArray<string>());
      newList.AddRange(rhs.ToArray<string>());
      return newList;
}
8
  • 2
    That is a dangerous experiment in the sense that if you get a habit of doing things like that in production, next thing some other guy does is to write listString3 = listString1 + listString2; and guess what happens. Nov 9, 2016 at 11:09
  • Well, first of all as I say it's just an experiment to understand what + operator would do in a List. But, what would happen it someone does listString3 = listString1 + listString2;? I think it would work as expected...
    – Pikoh
    Nov 9, 2016 at 11:12
  • 6
    Probably against the expectations of the author, listString1 will be changed, and changed again with any subsequent modification to listString3, because they will be the same list (List<> is a class). Could not be sure that this would be expected by the next guy, even if it is expected by you. Nov 9, 2016 at 11:15
  • 1
    I see. You are right, that could happen, but that's not a problem with the experiment, but of my implementation. I will edit to solve it
    – Pikoh
    Nov 9, 2016 at 11:20
  • 3
    Welcome to the reason why += is a better primitive than +: Writing + in terms of += costs nothing, writing += in terms of + requires duplicating the lhs needlessly. Sucks to be C#. Nov 9, 2016 at 15:28
15

The short answer is that operators in C# have to be overloaded for a given type. This also holds for +=. string contains an overload of this operator, but List<> has not. Therefore, using the += operator for lists is not possible. For delegates, the += operator is also overloaded, which is why you can use += on event handlers.

The slightly longer answer is that you are free to overload the operator yourself. However, you have to overload it for your own types, so creating an overload for List<T> is not possible, while you in fact can do this for your own class that for instance inherits from List<T>.

Technically, you do not really overload the +=-operator, but the + operator. The += operator is then inferred by combining the + operator with an assignment. For this to work, the + operator should be overloaded in such a way that the result type matches the type of the first argument, otherwise the C#-compiler is going to throw an error message when you try to use +=.

4
  • Thanks, so this is pretty conclusive then. The framework/language knows of no 'standard' way of addition/appending without a type-specific overload? Interesting.
    – user1017882
    Nov 9, 2016 at 9:30
  • 4
    @JᴀʏMᴇᴇ there is not generic concept of addition / appending in general
    – fixagon
    Nov 9, 2016 at 9:31
  • Just to say if you open the String class in reference code, the operator + is not overloaded. Nov 9, 2016 at 9:36
  • 1
    @mybirthname You will not find an explicit overload of the +-operator for strings, true and you won't find an explicit overload for int, double, etc. either. This is basically because these operator overloads are directly supported by the C#-compiler, much like the +-operator for delegates. Interestingly, decimal is the only standard numeric BCL class that has an explicit overload for the + operator.
    – Georg
    Nov 9, 2016 at 9:41
4

The correct implementation is actually quite a bit more complex than one would think. First of all, it is not sufficient to say that a += b is exactly the same as a = a+b. It has the same semantics in the simplest of cases, but it is not a simple text substitution.

First, if the expression on the left is more complex than a simple variable, it is only evaluated once. So M().a += b is not the same as M().a = M().a + b, as that would be assigning the value on a completely different object than it was taken from, or would cause the method's side effects to happen twice.

If M() returns a reference type, the compound assignment operator can be thought of as var obj = M(); obj.a = obj.a+b; (but still being an expression). However, if obj was of a value type, this simplification would also not work, in case the method returned a reference (new in C# 7) or if it actually was an array element, or something returned from an indexer etc., then the operator ensures that it doesn't make more copies than needed to modify the object, and it will be applied to a correct place, with no additional side effects.

Event assignment is an entirely different beast, though. Outside the class scope, += results in calling the add accessor, and -= results in calling the remove accessor of the event. If these accessors aren't user-implemented, event assignment might result in calling Delegate.Combine and Delegate.Remove on the internal delegate object inside the class scope. That's also why you cannot simply get the event object outside the class, because it is not public. +=/-= is also not an expression in this case.

I recommend reading Compound Assignment by Eric Lippert. It describes this in much more detail.

2
  • The expression M().a += b; would be illegal (compile-time error) is the declared return type of M was a value-type (struct) with a member a. The current implementation of the compiler uses the message error CS1612: Cannot modify the return value of 'YourType.M()' because it is not a variable I think you could be more clear about that. Nov 9, 2016 at 14:29
  • @JeppeStigNielsen Yes, I am mentioning it supposedly works only for ref-returning methods.
    – IS4
    Nov 9, 2016 at 20:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy