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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:57:08+00:00 2026-05-26T06:57:08+00:00

I was wondering when and why references get broken in C#? The following code

  • 0

I was wondering when and why references get broken in C#?

The following code example highlights this:

StringBuilder a = null, b = null;
a = new StringBuilder("a");
b = a;
b.Append("b");
b = null;
Console.WriteLine(a != null? a.ToString() : "null");
Console.WriteLine(b != null ? b.ToString() : "null");

//Output: 
    ab
    null

Why is it, for this example, that b‘s reference to a does not cause a to be null as well?

  • 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-26T06:57:08+00:00Added an answer on May 26, 2026 at 6:57 am

    You need to distinguish between variables, references and objects.

    This line:

    b = a;
    

    sets the value of b to the value of a. That value is a reference. It’s a reference to an object. At this point, making changes to that object via either a or b will just make changes to the same object – those changes would be visible via either a or b too, as both still have references to the same object.

    They’re like two pieces of paper with the same house address written on them though – the two variables don’t know anything about each other, they just happen to have the same value due to the previous line.

    So, when we change the value of b on this line:

    b = null;
    

    that just changes the value of b. It sets the value of b to a null reference. This makes no changes to either the value of a or the StringBuilder object which the old value of b referred to.

    I have an article which goes into more details on this, and which you may find useful.

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

Sidebar

Related Questions

I was wondering if someone could explain how the following code works: public interface
So the following code is possible in C# to start a new thread in
Just wondering if anyone has any favourite SQL references they to use when creating
I'm wondering if there's a super-efficient way of confirming that an Image object references
Wondering how to open many new windows with Javascript. I have found plenty of
I'm struggling to get mocks working, for a change, and was wondering where people
I'm trying to get a better understanding of how endpoints work. I've read this
I was wondering how people handle dealing with references across multiple users across multiple
Given you have the following class (bad C#, but you get the drift): public
My Solution/Project builds fine with code contracts turned off for this particular project. When

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.