Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need a .NET Dictionary<string,object> with just a little more functionality [closed]

Tags:

c#

.net

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.

like image 651
Ronnie Overby Avatar asked Mar 10 '26 03:03

Ronnie Overby


1 Answers

You can just use:

Dictionary<string, KeyValuePair<object,bool>>

This lets you store a KeyValuePair<object,bool>, which provides your custom class using a BCL type.

That being said, I often find myself using a custom class or struct for this, instead. Personally, I find that implementing a custom type for my value storage is often more maintainable over time than using a KeyValuePair, Tuple, or other non-specific type. Usage, a year later, is much more obvious if I have a custom type with an appropriate name.

like image 56
Reed Copsey Avatar answered Mar 12 '26 18:03

Reed Copsey



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!