5

Possible Duplicate:
Does C# optimize the concatenation of string literals?

I just found out that we write a line like this:

string s = "string";
s = s + s; // this translates to s = string.concat("string", "string");

However I opened the string class through reflector and I don't see where this + operator is overloaded? I can see that == and != are overloaded.

[TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]
    public static bool operator ==(string a, string b)
    {
      return string.Equals(a, b);
    }
[TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]
    public static bool operator !=(string a, string b)
    {
      return !string.Equals(a, b);
    }

So why does concat gets called when we use + for combining strings?

Thanks.

17
  • 2
    @MichaelPetrotta it's not a duplicate of that question. The linked question actually concerns constant folding. Commented Nov 29, 2012 at 2:09
  • 2
    @phoog, well, I kinda agree with you in that the question doesn't cover that, but Jon's answer answers VVV's question precisely. Commented Nov 29, 2012 at 2:11
  • 1
    @MichaelPetrotta, how does that answer by J. Skeet respond to this question? Commented Nov 29, 2012 at 2:56
  • 1
    If two strings were typed as dynamic, is the runtime hard-coded to treat the addition as String.Concat? Commented Nov 29, 2012 at 3:12
  • 2
    @MichaelPetrotta Sorry I've been unclear. I am trying to emphasize the fact that the answer to the present question is "section 7.7.4" but Jon's answer to the supposed duplicate question is "section 7.18". Jon's answer, and the question, are about constant folding, not the definition of the + operator. This question is about the definition of the + operator. The common answer to both questions is, indeed, "the compiler does it", but if we seek the answers to our "why" questions in the specs, then the answers are different and the questions are not duplicates. Commented Nov 29, 2012 at 4:36

2 Answers 2

6

So why does concat gets called when we use + for combining strings?

Section 7.7.4 of the C# specification, "Addition operator", defines a binary addition operator for strings, where the operator returns the concatenation of the operands.

The definition of System.String in the CLI specification includes several Concat overloads, but no + operator. (I don't have a definitive answer explaining that omission, but I suppose it's because some languages define operators other than + for string concatenation.)

Given these two facts, the most logical solution for the C# compiler writer is to emit a call to String.Concat when compiling the +(string, string) operator.

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

3 Comments

+1: for all the info...hope someone will give more facts about the issue
@KonstantinVasilcov I recall reading some Eric Lippert material on this question; I don't remember if it was here or on his blog, and I've spent too much time wallowing in the specs to start searching for that now. But I strongly recommend that you have a look yourself. Even if you don't find what you're looking for, you're sure to find something good. blogs.msdn.com/b/ericlippert and stackoverflow.com/users/88656/eric-lippert
Thanks! I do read Eric Lippert's blog)) Not quick enough, however)
5

The code

    public string Foo(string str1, string str2)
    {
        return str1 + str2;
    }

gives the following IL:

IL_0000:  nop
IL_0001:  ldarg.1
IL_0002:  ldarg.2
IL_0003:  call       string [mscorlib]System.String::Concat(string, string)
IL_0008:  stloc.0
IL_0009:  br.s       IL_000b
IL_000b:  ldloc.0
IL_000c:  ret

The compiler (at least, the one in Visual Studio 2010) does this job and there is no + overloading.

4 Comments

This answer merely confirms the behavior that the OP is asking about; it does not answer the question "why is it done that way?".
@phoog As you said yourself in one of your comment. The compiler has to implement a cast in a way or another as there is no overload for the + operator. Visual Studio compiler uses string.Concat. Why string.Concat rather than another implementation? I think it's because it's the simple thing to do but I don't know if the real answer is publicly available. So yes, my answer confirms OP's expectation. In some kind of way, + is just syntactic sugar like { get; set; }.
@phoog However your answer is obviously better as it refers to the specs and contains more "technical background". But it doesn't say neither why string.Concat rather than something else nor why + is not just simply overloaded.
Why + is not overloaded: it's hard to find a reason for not doing something. I never say to my boss "today, I refrained from writing a sort method because ...". Since I don't know the reason, I speculated. (I now have a vague recollection that I heard someone discuss that once in a Channel 9 video, but I am not sure about that.) Why string.Concat? I have come to the conclusion that it's technically an implementation detail, but it's hard to see how the author of a C# compiler could make any other choice and still be considered sane.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.