Are there performance disadvantages when I use a single-element array over a simple variable?
I have a base class from which many other classes are derived. This base class provides a Value property of type object.
Now I have to also support multi-value versions of most classes. Thus I was thinking about changing protected object value to protected object[] value and using the array even if only a single value is required.
I know the length of the array in the constructor, so in case of single-value instances, the length of the array is 1.
Since there ususally exist a lot of instances during runtime (up to several thousand, depending on number of users) I’m very concerned with performance.
So my question is: Will this change from simple variable to array cause a noticeable change in performance, or is it generally bad design?
I thought about creating a wrapper class that enables multiple values, but the classes are heavily connected via references and events, so this would be clumsy to use and maintain.
Edit To clarify:
The purpose of the base class is mainly to provide an interface. Other classes use the Value property, which is why it is defined in the base class. The variable itself was only defined in the base class so I don’t have to define it in every deriving class (type is always object).
The multi-value extension is not required because there are deriving classes that need more than one value, but because there will be a context-sensitive environment (don’t know how to phrase it better) and classes need to remember a value for every context.
For example, think of it as an iternationalization technique (not in my case, just as example).
A class represents a word and depending on the language, the Value is the word in the respective language.
By putting all the multi-value logic in the base class, deriving classes can continue to work like they always did (using the Value property) and the base class figures out which value to use.
Hope this makes things a bit clearer.
No: the amount of overhead would certainly not be noticeable. If it reduces the code’s complexity, then it is a good idea.