5

The System.String has only two Operator overloaded

public static bool operator ==(string a, string b)
{
  return string.Equals(a, b);
}

public static bool operator !=(string a, string b)
{
  return !string.Equals(a, b);
}

But when using += for String concat, Example :

    private static void Main()
    {
        String str = "Hello ";
        str += "World";

        Console.WriteLine(str);
    }

it works just fine,

So, how come if System.String doesn't overload the operator += it Concats the string?

2
  • 2
    Int doesn't overload addition either. So how can you add numbers? Commented Jan 23, 2013 at 6:33
  • @EricLippert - never checked the Int32 in reflector, this question was just for curiosity Commented Jan 23, 2013 at 6:37

4 Answers 4

8

First, the operator += can't be overloaded. If you have the expression A += B, it's compiled as if you wrote:*

A = A + B

Okay, that's why string doesn't overload operator += (because it can't be overloaded). So, why doesn't it overload operator + either? It's just one more difference between the CLR and C#. The C# compiler knows that types like string and int are special, and it generates special code for their operators (calling string.Concat() for string, or the add instruction for int).

Why are these operators treated in a special way? Because you want them treated in a special way. I think this is most clear for int:

  1. You don't want each int addition to be compiled as a method call, that would add a lot of overhead. Because of that, special instruction for int addition is used.
  2. Integer addition doesn't always behave the same with regards to overflows. There is a compiler switch to throw exceptions for overflows and you can also use the checked and unchecked operators. How should the compiler deal with that if it had only operator +? (What it actually does is to use the instruction add for unchecked overflows and add.ovf for checked overflows.)

And you want to treat string addition in a special way too, for performance reasons. For example, if you have strings a, b and c and write a + b + c and then you compiled that as two calls to operator +, you would need to allocate a temporary string for the result of a + b, which is inefficient. Instead, the compiler generates that code as string.Concat(a, b, c), which can directly allocate only one string of the required length.


* This is not exactly right, for details, see Eric Lippert's article Compound Assignment, Part One and Compound assignment in the C# specification. Also note the missing semicolons, A += B really is an expression, for example, you can write X += Y += Z;.

Sign up to request clarification or add additional context in comments.

Comments

5

There is no overload of += operator, its a short-hand for var = var + newValue. Same is the case with strings.

+= Operator (C# Reference)

The += operator cannot be overloaded directly, but user-defined types can overload the + operator

Consider the following example:

string str = "new string";
str += "new value";

This is equal to :

str = str + "new value";

which internally calls string.Concat at compile time.

2 Comments

This answer is misleading, and the last sentence is incorrect. The + operator is not overloaded on System.String; it is replaced at compile time with a call to string.Concat.
@codesparkle, I didn't say that + operator is overloaded in strings ??? What I meant was += is short hand for a = a + b, for strings. I believe I need to reword my answer
3

+= is not explicitly implemented but it works because compiler do it's magic

str += "World";

str =  str + "World";

str = str.Concat("World");

Comments

0

As the guys above said, and according to .NET, string += otherString is equivalent to

string = string + otherString

This .NET link mentions the concatenation operator, and this .NET link talks about the relationship between the two operations.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.