简体   繁体   English

C#-元组与字典和列表有什么区别

[英]C# - What is the difference between Tuple and Dictionary and List

This question already has answer here 这个问题已经在这里回答


I was trying to use Tuple in my application. 我试图在我的应用程序中使用Tuple。 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). 首先,AFAIK,没有列表带有两个类型参数-因此您的列表可以是List<int>List<string> (或SortedList<int,string> ,但这是一个不同的类)。

A Tuple<T1, T2> holds two values - one int and one string in this case. Tuple<T1, T2>包含两个值-在这种情况下为一个int和一个字符串。
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. 我个人讨厌Item1Item2属性名称,因此我可能在代码中使用围绕Tuples的包装类。

A Dictionary<TKey, TValue> is a collection of KeyValuePair<TKey, TValue> . Dictionary<TKey, TValue>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. 词典将键映射到值,这样您就可以拥有一个人词典,例如,每个人都有他们的SSN作为键。

A List<T> is a collection of T . List<T>是集合T
You can get individual items by using the index, the Find method, or simply linq (since it implements IEnumerable<T> ) 您可以使用索引, Find方法或简单地使用linq(因为它实现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: 引用Nikow的答案:

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 . 因此,对于DictionaryList的区别,您已经看到了链接到问题 So what is a Tuple ? 那么什么是Tuple By MSDN: 通过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. Tuple<T1, T2>类表示2元组或对,它是具有两个成分的元组。 A 2-tuple is similar to a KeyValuePair<TKey, TValue> structure. 2元组类似于KeyValuePair<TKey, TValue>结构。

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. 虽然其他两个数据结构表示不同的项目集合 (泛型为您声明的任何类型),但Tuple<T1,....>是包含预定义属性数量的单个项目 -类似于定义具有很少属性的类。

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. 当您链接的答案讨论List<T>Dictionary<int, T>之间的区别时, Tuple<int, string>

From MSDN 从MSDN

Represents a 2-tuple, or pair. 表示2元组或对。

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. 请注意 :从C#7开始,有一种更新,更简洁的语法用于返回元组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM