Given that there is a class called Point
with a float x
and float y
component.
class Point
{
public double x;
public double y;
public Point(double x, double y)
{
this.x = x;
this.y = y;
}
public Point()
{
x = y = 0;
}
public override string ToString()
{
return string.Format("({0:F2},{1:F2})", x, y);
}
}
Why in the mystery1
function didn't the point p1
get updated to be (11.00, 11.00) since p.x
is 11 from the first line?
{
Point p1 = new Point(11, 22);
Point p2 = p1;
int n1 = 33;
int n2 = n1;
mystery1(p1, n1);
Console.WriteLine("n1 = {0}, n2 = {1}", n1, n2);
Console.WriteLine("p1 = {0}, p2 = {1}", p1, p2);
mystery2(p2, n2);
Console.WriteLine("n1 = {0}, n2 = {1}", n1, n2);
Console.WriteLine("p1 = {0}, p2 = {1}", p1, p2);
}
static void mystery1(Point p, int n)
{
n = (int)p.x;
p = new Point(n, n);
}
static void mystery2(Point p, int n)
{
p.x = 77;
n = 88;
}