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.