This question already has answer here
I was trying to use Tuple in my application. So I want to differentiate the 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?
First, AFAIK, There is no 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 then 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>
)
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.
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
In addition you can have a look at What requirement was the tuple designed to solve?
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.