Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9181239
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T18:14:26+00:00 2026-06-17T18:14:26+00:00

The System.String has only two Operator overloaded public static bool operator ==(string a, string

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T18:14:27+00:00Added an answer on June 17, 2026 at 6:14 pm

    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;.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does System.String.Split() ever return null ? (.NET) I know I've been coding in the
Say I have a simple object which supports implicit casting to System.String public sealed
My co-worker is filling a System::String object with double-byte characters from an unmanaged library
Why does .Net not have the System.Float type like System.String , System.Double etc.?
I am getting a Method not found: 'Boolean MyCompany.LibraryAssembly.SomeFunction(System.String)' exception/error when running a VB.NET
How do a remove a instance of a System.String variable? $Servers = server,localhost I
I wanted to ask what is the idea behind the fact that System.String doesn't
Getting this weird LINQ error. title = System.Linq.Enumerable+WhereSelectEnumerableIterator`2[System.Xml.Linq.XElement,System.String Here is the code I have:
I create a constructor as follow Form1(array<System::String ^> ^args) //HW5 { InitializeComponent(); // //TODO:
So i'm trying to POST something to a webserver. System.Net.HttpWebRequest EventReq = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(url); System.String

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.