Back to questions
JuniorTheoryCommon
What does the program print?
Point a = new Point();
a.X = 1;
Point b = a;
b.X = 2;
Move(a);
Console.WriteLine(a.X + " " + b.X);
void Move(Point p) { p.X = 10; }
struct Point { public int X; }1 2
- Expecting
10 10:Pointis astruct, soPoint b = acopies the fields, andMovechanges its own copy of the point, nota. - Expecting
10 2: a method gets a copy of a struct argument; to change the caller's point it needsref Point pand the callMove(ref a).
- What would the program print if
Pointwere declared as aclass? - How would you change
Moveso that it moves the caller's point?