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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:19:56+00:00 2026-05-22T21:19:56+00:00

A few weeks ago, I discovered that strings in C# are defined as reference

  • 0

A few weeks ago, I discovered that strings in C# are defined as reference types and not value types. Initially I was confused about this, but then after some reading, I suddenly understood why it is important to store strings on the heap and not the stack – because it would be very inefficient to have a very large string that gets copied over an unpredictable number of stack frames. I completely accept this.

I feel that my understanding is almost complete, but there is one element that I am missing – what language feature do strings use to keep them immutable? To illustrate with a code example:

string valueA = "FirstValue";
string valueB = valueA;
valueA = "AnotherValue";

Assert.AreEqual("FirstValue", valueB); // Passes

I do not understand what language feature makes a copy of valueA when I assign it to valueB. Or perhaps, the reference to valueA does not change when I assign it to valueB, only valueA gets a new reference to itself when I set the string. As this is an instance type, I do not understand why this works.

I understand that you can overload, for example, the == and != operators, but I cannot seem to find any documentation on overloading the = operators. What is the explanation?

  • 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-05-22T21:19:57+00:00Added an answer on May 22, 2026 at 9:19 pm

    what language feature do strings use to keep them immutable?

    It is not a language feature. It is the way the class is defined.

    For example,

    class Integer {
        private readonly int value;
    
        public int Value { get { return this.value; } }
        public Integer(int value) { this.value = value; } }
        public Integer Add(Integer other) {
            return new Integer(this.value + other.value);
        }
    }
    

    is like an int except it’s a reference type, but it’s immutable. We defined it to be so. We can define it to be mutable too:

    class MutableInteger {
        private int value;
    
        public int Value { get { return this.value; } }
        public MutableInteger(int value) { this.value = value; } }
        public MutableInteger Add(MutableInteger other) {
            this.value = this.value + other.value;
            return this;
        } 
    }
    

    See?

    I do not understand what language feature makes a copy of valueA when I assign it to valueB.

    It doesn’t copy the string, it copies the reference. strings are reference type. This means that variables of type strings are storage locations whose values are references. In this case, their values are references to instances of string. When you assign a variable of type string to another of type string, the value is copied. In this case, the value is a reference and it is copied by the assignment. This is true for any reference type, not just string or only immutable reference types.

    Or perhaps, the reference to valueA does not change when I assign it to valueB, only valueA gets a new reference to itself when i set the string.

    Nope, the values of valueA and valueB refer to the same instance of string. Their values are references, and those values are equal. If you could somehow mutate* the instance of string referred to by valueA, the referrent of both valueA and valueB would see this mutation.

    As this is an instance type, I do not understand why this works.

    There is no such thing as an instance type.

    Basically, strings are reference types. But string are immutable. When you mutate a string, what happens is that you get a reference to a new string that is the result of the mutation to the already existing string.

    string s = "hello, world!";
    string t = s;
    string u = s.ToUpper();
    

    Here, s and t are variables whose values refer to the same instance of string. The referrent of s is not mutated by the call to String.ToUpper. Instead, s.ToUpper makes a mutation of the referrent of s and returns a reference to a new instance of string that it creates in the process of apply the mutation. We assign that reference to u.

    I understand that you can overload, for example, the == and != operators, but I cannot seem to find any documentation on overloading the = operators.

    You can’t overload =.

    * You can, with some tricks. Ignore them.

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

Sidebar

Related Questions

A few weeks ago I asked about Application Servers. It happens that my bosses
I asked about this on the jquery forum a few weeks ago without luck,
I posted about this a few weeks ago, but I don't think I asked
A few weeks ago, I asked a question about how to generate hierarchical XML
I used a query a few weeks ago in MySQL that described a table
I'm really frustrated with this one. A few weeks ago I got it working
a few weeks ago a co-worker of mine spent about two hours finding out
i asked this a few weeks ago, but couldnt get any of the suggested
A few weeks ago I asked a question about eliminating duplicate records in a
A few weeks ago I found somewhere a code that allows one to dump

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.