Question

(In the following code, player is a of a type which contains a Vector2 called Vector)

Vector2 v = player.Vector;
v.X -= player.Speed * (float)gameTime.ElapsedGameTime.TotalSeconds;
player.Vector = v;

vs.

player.Vector = new Vector2(player.Vector.X - player.Speed * (float)gameTime.ElapsedGameTime.TotalSeconds, player.Vector.Y);

These both accomplish the same task (Getting around the "Cannot modify the return value because it is not a variable" Error), but is one more efficient than the other?

Does one use less memory? (No, right?) Does one execute quicker? Is there a better way?

No correct solution

OTHER TIPS

The second option is better because it doesn't mutate any objects. To improve readability I'd extract a variable like this:

var x = player.Vector.X - player.Speed * (float)gameTime.ElapsedGameTime.TotalSeconds;
player.Vector = new Vector2(x, player.Vector.Y);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top