I have need to store multiple values in list or array for a moment. These values are multiple types like string, int, datetime etc.
I have two options:
Options 1:
Declare an array with type of object like this:
object[] values;
Option 2:
Declare array for interface and implement own classes for each type to hold.
interface IType
{
}
IType[] values;
class StringValue : IType
{
public string StringValue { get; set; } // not declared in interface!
}
class IntValue : IType
{
public int IntValue { get; set; } // not declared in interface!
}
Question:
What are performance benefit and/or impact for each implementation options? As far as I know (or quessing), boxinq has own performance impact when using object[].
What about option two? At least using StringValue or IntValue property needs more code, first type must be determined, IType must be casted to StringValue or IntValue etc…
Is there so big impact to performance using object[] that I really should think about something like options 2?
The class
IntValueyou propose essentially boxes an integer… it encapsulates it in an object to that it can participate in an object[]. I would expect no performance improvement with that scenario, and depending on your implementation, it could be worse than compiler-implemented boxing.Having said that, I agree with the commenters that your efforts are probably better focused improving other areas of your code. There are very few scenarios where the boxing overhead will be the most important area of attention.
I did encounter a scenario (working with many numbers that could be int, float, or double) where the boxing mattered to user experience. I solved that using expression trees.