Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Store non-unique pairs in .net 2.0?

Tags:

c#

.net

.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?

like image 673
Xaisoft Avatar asked Jan 22 '26 01:01

Xaisoft


1 Answers

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.

like image 67
BoltClock Avatar answered Jan 23 '26 14:01

BoltClock



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!