Home > Net >  Please answer
Please answer

Time:01-08

Class A {public int y; }
Struct B {public int x; The public A, A. }
Class Program {
The static void Set (B to B, int x, int y) {
B.x=x;
B.A.Y=y; }
The static void Main (String [] args) {
B B B=new {x=1, a=new a {y=1}};
Set (b, 2, 2);
System. The Console. WriteLine (b.x + ", "+ B.A.Y);
}
}


Why the output 1 and 2, you answer

CodePudding user response:

Is the value of the structure, there is no reference (reference),

CodePudding user response:

I know you where is the mistake of thinking, value type by value transfer is a copy of the reference type is passed the address, then according to the understanding of this sentence, what do you think of the results should be 1, 1, the output is the meaning?

CodePudding user response:

But this code is A special structure of A class object that is passed to the Set function is indeed A copy of the b, but the copy of the object and b itself A objects refer to the same address,

So to copy objects of A modified affect b itself A object,
  •  Tags:  
  • C#
  • Related