1
 namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            A a = new A();
            B b = new B(a);
            C c = new C(a);

            Console.WriteLine(a.intval + " " +a.strval);
            Console.ReadLine();
        }
    }

    class A
    {
        public int intval { get; set; }
        public string strval { get; set; }
    }
    class B
    {
        public A _a;
        public B(A a)
        {
            _a = a;
            _a.intval += 100;
            _a.strval += "From B;";
        }
    }
    class C
    {
        A _a;
        public C(A a)
        {
            _a = a;
            _a.intval += 1000;
            _a.strval += "From C;";
        }
    }
}

In above code, why the instance "b" and “c” could set value to instance "a"?

In my guess, the Class B and C have their own variables "a", when we call "B b = new B(a);" or "C c = new C(a);", the value should be changed on their own fields and the variable "a" in Main would be impacted?

But, i'm wrong. Could you please help me understand the result? Thank you very much.

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
CLearner
  • 13
  • 3

4 Answers4

1

It all makes sense when you understand what a variable is. A variable is a placeholder for a value, and what is this value?

  • If the variable type is a reference type, then the value stored inside the variable is simply the address in memory of where the instance it is currently pointing to resides.
  • If the variable type is a value type then the value stored inside the variable is the instance itself.

In your case, a's type is a reference type (class), so this means that the value stored in a is just the address where new A() lives.

Because arguments, by default, are passed by value in C#, when you do new B(a) or new C(a), a copy of the value in a is made, that is, a copy of the address. But all addresses, original or copied, point to the same object and that is why you are seeing the behavior you are seeing.

InBetween
  • 32,319
  • 3
  • 50
  • 90
0

A is a reference type (class here), so if you give the same ref of your a, change its values from b or c will alter a instance.

Tony THONG
  • 772
  • 5
  • 11
0

You create the object of class A as -

 A a = new A();

mean you have allocated a memory block that can be referenced as instance a and now you are passing same memory block address(a) to both class B and C.

 B b = new B(a);
 C c = new C(a);

mean here now both memory block b and memory block c points to the same memory block a, so either instance b or c change the value it will be reflected in same memory block allocated to instance a and then you are trying to get the value from instance a, so you will have updated value by both B and C

Deepak Sharma
  • 4,124
  • 1
  • 14
  • 31
0

Your class A is a reference type, so any changes to any instance of an A object will be shared across all other references of that same object.

Think of your variables as addresses to a place like, say, a park. When you create an instance of a reference type like so:

Foo a = new Foo();

then that is like writing the address to the park on a piece of paper. Then when you assign another variable to be equal to the first variable:

Foo b = a;

That is essentially copying the address onto another piece of paper. You now have two pieces of paper that are pointing to the same place (the park).

Then if someone were to use one of the pieces of paper to go to the park and change it somehow (like, say, build a new fountain):

b.Bar = 5;

that change would be reflected across all existing objects. Or in terms of the metaphor, anyone going to the park at that address would see the change:

Console.WriteLine(a.Bar);
// Console Output: 5

So when you are doing what you are doing in your code:

A a = new A();
B b = new B(a);
C c = new C(a);

Because A is a reference type, a, b._a, and c._a are all references to the same object. (They are all different pieces of paper, all with the same address to the same park.)

Abion47
  • 22,211
  • 4
  • 65
  • 88
  • Thank you so much for your example. It helps me get a better understanding of this concept. Thank you. – CLearner Apr 03 '17 at 10:53