Does anyone have any suggestions as to how to go about designing a container class, in C#, that has upwards of thirty variables contained within it?
I have written it out as a set of variables, as there are a few different types, e.g. string or DateTime, however I am thinking that it would perhaps be better to house them all in a dictionary of objects with their property names as keys?
If all the fields are required, then they are required. Using a dictionary can only add overhead and incur extra memory costs. A dictionary approach may be useful in a few specific scenarios, such as property bag implementations, and sparse event handlers (since events are the most obvious sparse data).
If the fields are used sparsely (i.e. often not more than a few are in use), then perhaps push the related fields into a number of inner classes (per group of related fields) – i.e. instead of:
something like:
Then when you have to store the user data, create a new
UserInfo; if you don’t need the user data, leaveusernull.