Reputation: 7493
I have code where I return a list of IWebElements and their corresponding names? My understanding is that a tuple with two items is basically the same thing but the Dictionary uses hash mapping to relate the two values. What is the advantage of using a Two Item Tuple over a Dictionary or vice versa?
public Dictionary<IWebElement, string> SelectAllOptions(IWebDriver driver, ref DataObject masterData)
{
//Get the ID of the dropdown menu
DatabaseRetrieval.GetObjectRepository(ref masterData);
var strDropMenuId = masterData.DictObjectRepository["ID"];
//Find the dropdown menu and pull all options into a list
try
{
var dropMenu = new SelectElement(driver.FindElement(By.Id(strDropMenuId)));
// TODO want to know how we want this list to return.
var options = dropMenu.Options as List<IWebElement>;
if (options != null)
{
var values = options.ToDictionary(option => option, option => option.Text);
return values;
}
}
Upvotes: 13
Views: 11310
Reputation: 919
One advantage of tuples over dictionaries is that tuples can be named.
Using List<(string text, string url)> links
is more meaningful than Dictionary<string, string> links
.
Upvotes: 2
Reputation: 149538
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
)
Upvotes: 11
Reputation: 4402
To add to the other answers, there are sometimes advantages to storing key-value data as a list of tuples instead of in a dictionary.
Depending on your needs, it might not be important that your lookups are fast, but it might be important that the order you insert into the list remains fixed. You can iterate through the keys and the values in a dictionary but the order is not defined.
Another advantage is that you can put as many pairs with the same first element into the list as you want, where with a dictionary you can only have one value per unique key.
Upvotes: 2
Reputation: 726569
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(...)
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.
Upvotes: 26