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 8661719
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T16:33:55+00:00 2026-06-12T16:33:55+00:00

From Richter and this discussion , I would expect any two identical strings to

  • 0

From Richter and this discussion, I would expect any two “identical” strings to be the same reference. But just now in LINQPad I got mixed results on this topic. Here is the code:

void Main()
{
    string alpha = String.Format("Hello{0}", 5);
    string brava = String.Format("Hello{0}", 5);
    ReferenceEquals(alpha, brava).Dump();
    String.IsInterned(alpha).Dump();
    String.IsInterned(brava).Dump();

    alpha = "hello";
    brava = "hello";
    ReferenceEquals(alpha, brava).Dump();
}

And here are the results from the Dump() calls:

False
Hello5
Hello5
True

I would have expected both the first and last ReferenceEquals to be True. What happened?

Besides the example above, in what other cases would the ReferenceEquals fail? For example, multi-threading?

This issue is important if, for example, I’m using string parameters passed into a method as the object upon which a lock is taken. The references better be the same in that case!!!

  • 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-12T16:33:56+00:00Added an answer on June 12, 2026 at 4:33 pm

    This blog entry explains why.

    In short, if your string isn’t allocated through ldstr (i.e. it’s not a string literal defined within your code), it doesn’t end up in the (hash) table of interned strings, and therefore interning doesn’t occur.

    The solution is to call String.Intern(str). The Intern method uses the intern pool to search for a string equal to the value of str. If such a string exists, its reference in the intern pool is returned. If the string does not exist, a reference to str is added to the intern pool, then that reference is returned.

    Don’t lock on strings, especially if you’re attempting to use two different reference variables to attempt to point to the same (possibly) interned string.

    Also note that there are some disadvantages to interning strings. Because string literals are not expected to change during the program’s lifetime, interned strings are not garbage collected until your program exits.

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

Sidebar

Related Questions

I don't know what exactly this feature is, but I would like to simulate
An interesting thread came up when I typed in this question just now. I
From this context: import itertools lines = itertools.cycle(open('filename')) I'm wondering how I can implement
From time to time, I find myself using the same group of statements and
I have been playing with the demo code from this msdn article by Jeffrey
From the Susy docs: http://susy.oddbird.net/guides/reference/#ref-grid-background SUSY GRID BACKGROUND Show the Susy Grid as a
From an alphanumeric string I need to pick all two digit numbers. e.g.: input
If I use the following call in C++, I would expect the WorkingSet of
I'm searching for this for quite some time now. I saw few similar questions
From c++ FAQ: http://www.parashift.com/c++-faq-lite/dtors.html#faq-11.9 Remember: delete p does two things: it calls the destructor

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.