BText
BText

Reputation: 3

How would I store this data?

I have some data that goes over three levels, like this:

Identifier, Key, Value

I would like to first look up the Identifier. Let's say it's 1. Then I would look up the key, let's say 2. Finally, the key would give me the value.

I guess I might need multi-value dictionaries or some two/three dimensional arrays. All the data is fixed, with only 6 elements needed for an array.

So, I would like something in code that can be accessed like this:

SomeInfo[identifier, key] which would return value.

Please ask for more info.

Upvotes: 0

Views: 78

Answers (1)

Kirk Woll
Kirk Woll

Reputation: 77546

Use the Tuple class, and make the Tuple your key. That way your key can be composed of multiple values:

SomeInfo[new Tuple<string, string>(identifier, key)];

To elaborate, the Tuple class provides a proper implementation of GetHashCode() and Equals() that respects the values you pass in. And if "identifier" or "key" are not string, you can change the type arguments you pass into it.

Edit: Here's a working example:

var dictionary = new Dictionary<Tuple<string, string>, string>();
dictionary[new Tuple<string, string>("myIdentifier", "myKey")] = "myValue";

Upvotes: 5

Related Questions