What is the best way to store pairs of strings, make an object or use a class in .NET?

The “pair” generic class for .NET is Tuple. You would use it like:

var strings=new List<Tuple<string, string>>();
strings.Add(Tuple.Create("REFERENCE", "Ref"));

A dictionary is a perfectly acceptable substitute if the left-most string is unique (ie a key). You’ll get errors otherwise.

As to whether it’s better to use the built-in collections or create an actual object, depends on your needs (will you be adding more columns later on? you can’t do that with a dictionary approach), how often you use it (if it’s a core type, you should probably make a domain model for it) etc.

Edit: As to not using any dictionary built-in functionality, that’s not true: you’re using its binary search algorithm and internal tree construction for lightning-fast look-ups. A list of either Tuple or your own type most likely won’t have this and it will revert to a linear search.

Leave a Comment

tech