dimanche 30 mars 2014

C# call by value/ref + extension methods


Vote count:

0





public static class RectangleExtension
{
public static Rectangle Offseted(this Rectangle rect, int x, int y)
{
rect.X += x;
rect.Y += y;
return rect;
}
}


....

public void foo()
{
Rectangle rect;

rect = new Rectangle(0, 0, 20, 20);
Console.WriteLine("1: " + rect.X + "; " + rect.Y);

rect.Offseted(50, 50);
Console.WriteLine("2: " + rect.X + "; " + rect.Y);

rect = rect.Offseted(50, 50);
Console.WriteLine("3: " + rect.X + "; " + rect.Y);
}


Output:


1: 0; 0


2: 0; 0


3: 50; 50


What I expected:


1: 0; 0


2: 50; 50


why does rect.Offseted(50, 50) not modify the x and y of the rectangle in step 2?



asked 34 secs ago






Aucun commentaire:

Enregistrer un commentaire