views:

42

answers:

1

In a method that gets called frequently, like a painting event, is it more efficient to reuse Point and Rectangle Objects (for specifying locations and bounds), or should I create new ones.

+3  A: 

Well, Point and Rectangle aren't objects - they are structs. So they don't have a heap presence (unless they are on a field), but conversely copying them does possibly have an impact (not a huge one - they aren't very big).

If you are using them in a tight loop, then fine - pre-initialise them in a variable. Of course, you could just refactor your current x / y and width / height to use the Point etc for storage. Note also that (contrary to most scenarios) they are actually mutable structs, so you can change their internal values inside your loop, etc.

Also note that IIRC there are overloads of many of the graphics operations that take the primitive values instead of the structs - have you considered those?

Ultimately, though, I don't think this is going to have any noticeable impact compared to the actual graphics operations. Don't stress it; if your code works...

Marc Gravell