Question

I have a question about XNA which I am new too and could not find a clear answer for in the documentation after searching google.

Question 1:

If I do this:

Rectangle afterMoveRect = collisionRectangle;
afterMoveRect.Offset((int)moveAmount.X, (int)moveAmount.Y);

Did I now:

A. refference collisionRectangle with another name and afterwards moved collisionRectangle

or

B. Created a duplicate copy of collisionRectangle and only moved the copy.

Second question:

If I do this:

Matrix transform = localTransform;

transform = transform * otherTransform;

Did I create a copy of localTransform and mulitplied the copy or did I reference localTransform and multiplied it through another identifier?

The question for both is the same, did I change the original (perhaps unintentionally)

Question 3:

How do you know if you changed the original or not? How do you know if you reference the original with a new name (like in Java) or create a deep copy of the other object when using the assignment operator?

Was it helpful?

Solution

Depends entirely if Rectangle or Matrix is a struct or a class.

If it is a struct (a value type), it will be copied, you are working on a different instance. If it is a class (a reference type) it will be referenced, you are working on the same object.

According to the msdn, both Rectangle and Matrix are structs, so you are working on copies.

OTHER TIPS

It's called reference and value types. Reference types (classes basically) are not created when given as a value, only a reference is pointed, so in that cas, the modification of the new name will affect the original. In case of value types (structs) the copy is done, and the two is handled independently afterwards.

Both Matrix and Rectangle are structs, they got copied.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top