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

  • SEARCH
  • Home
  • 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 7952799
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T02:58:06+00:00 2026-06-04T02:58:06+00:00

I know that string is immutable and StringBuilder is mutable. But can anybody explain

  • 0

I know that string is immutable and StringBuilder is mutable. But can anybody explain following code output? Since both are reference types, why do they have different results?

String s1 = "hello";
String s2 = "hello";
Console.WriteLine(s1 == s2); //true
Console.WriteLine(Object.ReferenceEquals(s1, s2)); //true

StringBuilder sb1 = new StringBuilder("hello");
StringBuilder sb2 = new StringBuilder("hello");
Console.WriteLine(sb1 == sb2); //false
Console.WriteLine(Object.ReferenceEquals(sb1,  sb2)); //false
  • 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-04T02:58:08+00:00Added an answer on June 4, 2026 at 2:58 am

    Since both are reference types, why do they have different results?

    Because string objects are highly optimized. In particular, since they’re immutable, they can be interned by the compiler to prevent duplication.

    If you have two different string objects that both represent the exact same string of characters (as in your example), the compiler will recognize that and maintain only one instance of the actual string object.

    The result is that both the s1 and s2 objects actually are the same object as far as the compiler is concerned and even reference the same location in memory.

    This bookkeeping happens behind the scenes in something called an “intern table”, but that’s not really something you need to worry yourself with. The important thing is that all string literals are interned by default by the compiler.

    The same kind of thing does not happen for StringBuilder objects, because they are not immutable. They’re designed to allow you to modify a string object, and as such, the optimizations don’t make much sense. That’s why your sb1 and sb2 objects are actually seen as two different objects.

    The rule of thumb is quite simple: Use string by default, or when you want a single immutable string of characters. Only use StringBuilder when you want to modify the same string multiple times, say in a loop or other relatively short section of code.

    Relevant reading: Optimizing C# String Performance

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

Sidebar

Related Questions

i know that .NET String class uses such behaviour, but I can't find any
I know that StringBuilder is more efficient than a normal string when processing code
I know that %s is a string of characters, but I don't know how
I know that using Dim currUser As String = Request.ServerVariables(LOGON_USER) returns the Domain\Username, but
I know that I can pass a string as the second parameter to the
I know that in J2ME I can get a byte[] object from a String
I know that for an integer, you can use: int value; I tried: string
Possible Duplicate: String vs StringBuilder i know .NET Strings are immutable which is the
I know that Strings are immutable. Then, what is the difference between: String name
As we all know, String is immutable in java. however, one can change it

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.