Search code examples
c#.net.net-2.0

Store non-unique pairs in .net 2.0?


I have to store a pair of strings that are non-unique, so I could have something like:

"A", "B" "A","C"

I can't use a Dictionary or HashTable because I don't have a unique key. I thought about a couple options:

List<List<string>,List<string>> = new List<List<string>,List<string>>(); //Too long?

Create a class or struct (what is better?) to hold my pairs and then store that in list, so

class pairs
{

}

List<pairs> values = new List<pairs>();

I also just need to be able to enumerate through the pairs, so I don't need care about querying, sorting, comparing. Because of this, is it best to use the least specific type, such as Enumerable?


Solution

  • You should be able to use a List<KeyValuePair<string, string>>.

    KeyValuePair<TKey, TValue> structs by themselves don't require unique keys, even if you collect them in a single list.

    If you think KVPs don't ring well semantically with what you want, just go with a custom type. It shouldn't matter whether your custom type is a class or struct.