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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T22:38:05+00:00 2026-06-09T22:38:05+00:00

In the code below, I’m checking the equality of object references. string x =

  • 0

In the code below, I’m checking the equality of object references.

string x = "Some Text";
string y = "Some Other Text";
string z = "Some Text";

Console.WriteLine(object.ReferenceEquals(x, y)); // False
Console.WriteLine(object.ReferenceEquals(x, z)); // True
Console.WriteLine(object.ReferenceEquals(y, z)); // False

y = "Some Text";

Console.WriteLine(object.ReferenceEquals(x, y)); // True
Console.WriteLine(object.ReferenceEquals(x, z)); // True
Console.WriteLine(object.ReferenceEquals(y, z)); // True

Here:

  • x and z refers to same object; I can say that x is interned and z is used taht version. Well, I’m not sure about this; Please correct me, if I am wrong.
  • I changed the value of y by assigning it the same value as x. I thought it is going to create a new object here; but I was wrong, it used the same reference.

My questions are:

  • Does .net uses string interns for every string that I use?
  • If so, isn’t it hurts the performance?
  • If not, how the references became same in above example?
  • 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-09T22:38:07+00:00Added an answer on June 9, 2026 at 10:38 pm

    Yes, constant string expressions in the compiler are treated with ldstr, which guarantees interning (via MSDN):

    The Common Language Infrastructure (CLI) guarantees that the result of two ldstr instructions referring to two metadata tokens that have the same sequence of characters return precisely the same string object (a process known as “string interning”).

    This isn’t every string; it is constant string expressions in your code. For example:

    string s = "abc" + "def";
    

    is only 1 string expression – the IL will be a ldstr on “abcdef” (the compiler can compute the composed expression).

    This does not hurt performance.

    Strings generated at runtime are not interned automatically, for example:

    int i = GetValue();
    string s = "abc" + i;
    

    Here, “abc” is interned, but “abc8” is not. Also note that:

    char[] chars = {'a','b','c'};
    string s = new string(chars);
    string t = "abc";
    

    note that s and t are different references (the literal (assigned to t) is interned, but the new string (assigned to s) is not).

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

Sidebar

Related Questions

The code below simply didn't work. document.getElementById('files').addEventListener('change', handleFileSelect, false); reported by firebug that this
My code below kinda works, it creates the User object and saves but it
Code below contains a JS function that receives as parameter String €€ (Euro sign).
code below. i'm tryind to obtain string answers like a1, c4 this is what
Code below: [HttpGet] public ActionResult Edit(string id=) { // ... } [HttpPost] public ActionResult
Code below from other stackoverflow answer is used in jqGrid to implement checkbox using
The code below creates a form and styles the submit button according to some
Code below is working well as long as I have class ClassSameAssembly in same
Code below is used to save PostgreSql database backup from browser in Apache Mono
This code below allows me to find the word error in all my files

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.