What is the difference between Tuple, Dictionary and List in C#?
Solution 1
First, AFAIK, there isn't any list that takes two type parameters - so your List could either be a List<int>
or List<string>
(or a SortedList<int,string>
, but that's a different class).
A Tuple<T1, T2>
holds two values - one int and one string in this case. You can use a Tuple to bind together items (that might be of different
types). This may be useful in many situations, like, for instance, you want to return more than one value from a method.
I personally hate the Item1
, Item2
property names, so I would probably use a wrapper class around Tuples in my code.
A Dictionary<TKey, TValue>
is a collection of KeyValuePair<TKey, TValue>
. A Dictionary maps keys to values, so that you can have, for instance, a dictionary of people and for each person have their SSN as a key.
A List<T>
is a collection of T
.
You can get individual items by using the index, the Find
method, or simply LINQ (since it implements IEnumerable<T>
).
Solution 2
While the answer you have linked discusses the differentiation between List<T>
and Dictionary<int, T>
, Tuple<int, string>
is not addressed.
From MSDN
Represents a 2-tuple, or pair.
A tuple is a pair of values, opposed to the other types, which represent sort of collections. Think of a method, which returns an error code and string for the last error (I would not write the code this way, but to get the idea)
Tuple<int, string> GetLastError()
{
...
}
Now you can use it like
var lastError = GetLastError();
Console.WriteLine($"Errorcode: {lastError.Item1}, Message: {lastError.Item2}");
This way you do not have to create a class, if you want to return a compound value.
Please Note: As of C# 7 there is a newer, more concise syntax for returning tuples.
Solution 3
A list can store a sequence of objects in a certain order such that you can index into the list, or iterate over the list. List is a mutable type meaning that lists can be modified after they have been created.
A tuple is similar to a list except it is immutable. There is also a semantic difference between a list and a tuple. To quote Nikow's answer:
Tuples have structure, lists have order.
A dictionary is a key-value store. It is not ordered and it requires that the keys are hashable. It is fast for lookups by key.
Solution 4
So for the difference between a Dictionary
and a List
you've seen the question you linked to. So what is a Tuple
? By MSDN:
A tuple is a data structure that has a specific number and sequence of values. The
Tuple<T1, T2>
class represents a 2-tuple, or pair, which is a tuple that has two components. A 2-tuple is similar to aKeyValuePair<TKey, TValue>
structure.
While the two other data structures represent different collections of items (generic to be whatever type you declare) a Tuple<T1,....>
is a single item containing a predefined amount of properties - similar to defining a class with few properties.
As the two first ones are collections of items and a tuple is a single item they play very different roles in terms of what to do with them.
Read more on
- MSDN - common uses for tuples
In addition you can have a look at What requirement was the tuple designed to solve?
Iswar
I am .Net Core Programmer with experience in Angular 7, SQL Server, Bootstrap framework and have a desire to learn
Updated on January 14, 2022Comments
-
Iswar over 2 years
This question already has an answer here.
I was trying to use Tuple in my application. So I want to differentiate a Tuple, Dictionary and List.
Dictionary<int, string> dic = new Dictionary<int, string>(); Tuple<int, string> tuple = new Tuple<int, string>(); List<int> list = new List<int>();
What is the difference between these three?
-
digaomatias about 2 yearsI think in C# 8 tuples got much better in the sense that you don't need to see Item1 and Item2 anymore. You just give a name. And instead of having to create Tuple<T1, T2>, you can just define your type as (T1 Name1, T2 Name2). Instead of Tuple<int, string> tuple = new Tuple<int, string>(); You do: (int id, string value) tuple = (myId, myValue);
-
Zohar Peled about 2 years@digaomatias There are also other differences between
System.Tuple
andSyste.ValueTuple
.