What is the advantage of using a Two Item Tuple versus a Dictionary?
As a general rule, you do not want to "pay" * for possibilities that your program does not need. For example, if your program is interested in retrieving and processing a sequence of pairs (also known as "two-member tuples") but it does not need to perform lookups from the first member of a tuple to the second, then providing a collection of pairs is more efficient:
IEnumerable<Tuple<IWebElement, string>> SelectAllOptions(...)
- This approach takes less memory, because you do not allocate space for hash buckets
- This approach takes less CPU, because there is no hash key computation or collision resolution costs
- This approach does not suggest to a reader that the data is intended for lookups.
Of course if the data structure that you return is intended for lookups, then you should either return a dictionary, or construct one on the client side to transfer some of the CPU load from the server to the client.
* With memory, CPU cycles, decreased readability, etc.
A Tuple<T1, T2>
represents a pair of values. That pair don't necessarily have to mean "These two items are related". When using a KeyValuePair<TKey, TValue>
, you would expect that given a key, you would get a value and the two would have some sort of connection between one another.
Tuples implement IComparable
and IStructuralEquatable
, which makes it easier to compare Tuples.
Other than that, I would look at it from a logical perspective, do I need to match a given key to a value?, or do I just need to couple together two values and a class might be a bit of an overhead for that.
One downside of Tuples as I see it, is that you have to deal with properties labeled Item1 and Item2, which might make it a bit less readable.
Also, remember that a Tuple is a class
(an Immutable one) and KeyValuePair
is a struct, so when you passing them as arguments you pass Tuple by reference and KeyValuePair by value (except for explicitly declaring ref
or out
)