I need a dictionary but I also need to store a boolean value about the object in the dictionary. What’s the best way for this.
Something like Dictonary<string,object,bool> would be ideal, but doesn’t exist.
My first idea was:
public class SomeObject
{
public object Value { get; set; }
public bool Flag { get; set; }
}
// and then use:
Dictionary<string,SomeObject> myDictionary;
My 2nd idea was to implement IDictionary<string,object> and contain two dictionaries within that were manipulated by the implemented methods and property accessors. They would have to be kept in sync:
class DataDictionary : IDictionary<string, object>
{
private Dictionary<string, bool> _clientSideFlags = new Dictionary<string, bool>();
private Dictionary<string, object> _data = new Dictionary<string, object>();
// implemented methods, etc.
}
My 3rd idea was to see what the folks at StackOverflow would do.
Go with your first idea – it is simple, effective, and easily understood. Yes, the custom type may feel like overkill but it increases the readability of you code. Anyone who maintains this code later will thank you.