Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Dictionary with two Values per Key?

I have a situation in code where a Dictionary<string, string> seemed like the best idea - I need a collection of these objects and I need them to be accessible via a unique key. Exactly what the Dictionary concept is for, right?

Well, the requirements have expanded to the point where I now need to hold an additional bit of information per-key (a boolean value, if you're curious).

So, I figure expand the concept to create a new data structure with the string and the boolean and have it now be a Dictionary<string, NewCustomObject>.

However, for just one additional value like a boolean flag, it just feels like overkill. And yet I don't know of any Dictionary-like generic object with two values per key.

Is just having a Dictionary of custom objects the best way to go about this or is there something simpler for this scenario?

like image 551
Tom Kidd Avatar asked Sep 30 '09 21:09

Tom Kidd


People also ask

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

Why is C named so?

Quote from wikipedia: "A successor to the programming language B, C was originally developed at Bell Labs by Dennis Ritchie between 1972 and 1973 to construct utilities running on Unix." The creators want that everyone "see" his language. So he named it "C".

What meant by C?

noun plural c's, C's or Cs. the third letter and second consonant of the modern English alphabet. a speech sound represented by this letter, in English usually either a voiceless alveolar fricative, as in cigar, or a voiceless velar stop, as in case.


2 Answers

Actually, what you've just described is an ideal use for the Dictionary collection. It's supposed to contain key:value pairs, regardless of the type of value. By making the value its own class, you'll be able to extend it easily in the future, should the need arise.

like image 100
Traveling Tech Guy Avatar answered Sep 22 '22 18:09

Traveling Tech Guy


class MappedValue {     public string SomeString { get; set; }     public bool SomeBool { get; set; } }  Dictionary<string, MappedValue> myList = new Dictionary<string, MappedValue>; 
like image 32
Ed S. Avatar answered Sep 23 '22 18:09

Ed S.