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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T22:57:24+00:00 2026-05-15T22:57:24+00:00

I’ve been playing around trying to thoroughly understand Reference and Value types. Just when

  • 0

I’ve been playing around trying to thoroughly understand Reference and Value types. Just when I thought I had it, I came across this scenario…

I created a class that would contain a single object.

class Container
{
    public object A {get; set;}
}

When I create an instance of this Container class (a) I am creating instance of a reference type. I assign an integer to the object within the class. As far as I am aware, this will be boxed as an object, another reference type.

int start = 1;  
Container a = new Container{ A=start };

I create another instance of the Container class (b), but assign the value of the first container to it, the value of b is now a reference to a.

Container b = a;

As expected when I print out the value of both a.A and b.A, they are the same.

Console.WriteLine("a.A={0},b.A={1}",a.A,b.A);
//a.A=1,b.A=1

And, as expected, when I change the value of a.A the value of b.A also changes due to them referencing the same object.

a.A = 2;

Console.WriteLine("a.A={0},b.A={1}",a.A,b.A);
// a.A=2,b.A=2

Now I decided to try this using individual local objects. Again, I box the integer into the first object and assign the value of the first object to the second. I believe the object, at this point, should be a reference type so c and d should reference the same object. Without changing anything they return the same value.

int start = 1;
object c = start;
object d = c;

Console.WriteLine("c={0},d={1}",c,d);
// c=1,d=1

Like before, when changing the value of the initial object, I expect the value of both objects to be the same.

c = 2;

Console.WriteLine("c={0},d={1}",c,d);
// c=2,d=1

When I print the result for these two objects, the value of d does not change like before.

Can someone please explain why the assignment is different in this scenario to the previous?

Thanks

  • 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-15T22:57:24+00:00Added an answer on May 15, 2026 at 10:57 pm

    Here’s your first mistake:

    I create another instance of the Container class (b), but assign the value of the first
    container to it, the value of b is now a reference to a.

    Container b = a;
    

    That’s not creating another instance. It’s declaring another variable. Now both variables refer to the same object.

    (I’ll keep editing this answer as I keep reading…)

    Next up:

    int start = 1;
    object c = start;
    object d = c;
    Console.WriteLine("c={0},d={1}",c,d); // c=1,d=1
    

    Like before, when changing the value of the initial object, I
    expect the value of both objects to be the same.

    c = 2;
    Console.WriteLine("c={0},d={1}",c,d); // c=2,d=1
    

    That’s not changing an object it’s changing a variable. Each of the assignments copies a value – except one of them also performs a boxing operation. Let’s simplify it slightly:

    object c = "first string";
    object d = c;
    

    Now there’s no boxing involved – it’ll just make it simpler to understand.

    Both variables currently have values referring to the same object. That’s due to the assignment, but there’s nothing else linking the two variables. They happen to have the same value at the moment, but they’re independent variables. Now let’s change one:

    c = "different string";
    

    That has changed the value of c to refer to a different object. If you print out the values of c and d they will be “different string” and “first string” respectively. Changing the value of c doesn’t change the value of d.


    Now, let’s go back to your previous scenario to see why it’s different. There, you had:

    a.A = 2;
    

    That isn’t changing the value of a at all. It’s changing the data within the object a refers to.

    Let’s use a real world analogy to make this easier. Suppose all our variables are pieces of paper with house addresses written on them. The change of a.A = 2; is like changing the contents of the house at that address. Of course anyone else with the same address written on their piece of paper will see the change.

    Now think of your c/d scenario. Again, imagine that we have two pieces of paper, and due to the assignment operator they both have the same address written on them. Now your assignment of a new value to the c variable itself is like scrubbing out the address on the c piece of paper and writing a different address on it. That doesn’t change the d piece of paper at all – it still has the old address on, and if you visit that house, you’ll see that nothing’s changed. It’s only what’s written on the piece of paper that’s changed.

    Does that help?

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

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to select an H1 element which is the second-child in its group
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

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.