I'm currently reading Oreilly's Learning XNA 4.0 and I've noticed that the author is prone to creating a struct value, such as Vector2
or Rectangle
multiple times with no regard for performance.
For example, a SquareCollision()
method, that checks the collision between two game entities creates two Rectangle
s every time it is called, instead of simply having each game entity hold its own Rectangle
field and compare the fields without creating any more struct values.
I've seen this pattern many times in many places and that made me wonder:
Is the creation of struct values that negligible in C#?
Does this really fall under the micro-optimizations section?
Perhaps because my experience is mostly with Java, the concept of ValueType
s is strange to me, but it seems that recreating a value from a struct (and calling it's constructor) multiple times seems like a giant waste of resources.