Graham Derreck

Graham Derreck

  • NA
  • 3
  • 2.8k

Object Creation

Jan 16 2011 7:58 AM
I am a hobbyist, so you'll forgive me if the question seems basic. I am writing the inevitable calculator program. My question, is how to handle the creation of objects. C# is OOP after all. I created classes to perform various actions. Data input, calculation, etc. It seems to me that if I create an object every time there is a keystroke, the garbage collector will be busy indeed. I realize that is its job, but is that good practice? I set repetitive, concrete tasks: addition, square root, et al, as static. I also created data items that had to persist throughout the life of the program, as static. I then thought: why not use structs where there are methods and no data that had to have initial values assigned? They can be initialized without having to create an object on the heap and are not required to be static to be used without instances. I am obviously exposing my ignorance of what constitutes excessive object creation. There is a question in here, honest. So with the view that each keystroke could create an object:

create an instance for each action
use structs where possible
use classes with static members

I realize that the selection of the the above options is simplistic on my part, but I am looking for guidelines as opposed to syntax.

Thank you so much, in advance, for your help.



Answers (3)