The question is: at the end of this code the value of ptArray[0].X is 3.33 or 1.11?
Thanks.
class MyPoint
{
public double X, Y;
public MyPoint(double x, double y)
{
X = x;
Y = y;
}
}
MyPoint[] ptArray = new MyPoint[2];
ptArray[0] = new MyPoint(1.11, 2.22);
MyPoint first = ptArray[0];
// Am I changing ptArray[0] here or not?
first.X = 3.33;
first.Y = 4.44;
You’re not changing
ptArray[0]itself, because that’s a reference to the instance ofMyPoint. However, you are changing the data within the object that it’s referring to. So if you do:it will indeed print out 3.33.
Note that this wouldn’t be true if
MyPointwere a struct instead of a class. Although having mutable structs is a whole other realm of pain…