Tuple和keyvaluepair有什么区别

时间:2021-08-13 13:28:24

I am not understanding the difference between tuple and keyvaluepair. i was looking into this article http://www.dotnetperls.com/tuple-keyvaluepair. it seems like tuple is better than keyvaluepair but some of the developers saying strictly do not use tuple i wonder why?

我不理解元组和keyvaluepair之间的区别。我正在研究这篇文章http://www.dotnetperls.com/tuple-keyvaluepair。看起来像元组比keyvaluepair好,但有些开发人员说严格不使用元组我想知道为什么?

4 个解决方案

#1


0  

I think those programmers are saying not to use Tuples because on a public API, they are less effective.

我认为那些程序员说不使用元组,因为在公共API上,它们效率较低。

Lets say you have a tuple like

让我们说你有一个像元组

Tuple<int,string,string,int>

then how will you identify that what are these values later or what they are representing. I think thats why programmers say that. Like in this case the consumer has to either guess or look up documentation to know what you mean.

那么你将如何确定这些值以后是什么或它们代表什么。我想这就是程序员为什么这么说的原因。就像在这种情况下,消费者必须猜测或查找文档以了解您的意思。

The MSDN says:

MSDN说:

A tuple is a data structure that has a specific number and sequence of elements.

元组是具有特定数量和元素序列的数据结构。

Tuples are commonly used in four ways:

元组通常以四种方式使用:

  • To represent a single set of data. For example, a tuple can represent a database record, and its components can represent individual fields of the record.
  • 表示单组数据。例如,元组可以表示数据库记录,其组件可以表示记录的各个字段。
  • To provide easy access to, and manipulation of, a data set.
  • 提供对数据集的轻松访问和操作。
  • To return multiple values from a method without using out parameters (in C#) or ByRef parameters (in Visual Basic).
  • 从方法返回多个值而不使用out参数(在C#中)或ByRef参数(在Visual Basic中)。
  • To pass multiple values to a method through a single parameter. For example, the Thread.Start(Object) method has a single parameter that
    lets you supply one value to the method that the thread executes at
    startup time. If you supply a Tuple object as the method argument,
    you can supply the thread’s startup routine with three items of data.
  • 通过单个参数将多个值传递给方法。例如,Thread.Start(Object)方法有一个参数,允许您为线程在启动时执行的方法提供一个值。如果提供Tuple对象作为方法参数,则可以为线程的启动例程提供三项数据。

#2


3  

There are quite a few people that call themselves developers that should not. Having said that. The link that you posted goes into some detail about the differences and when you should use them. In the end it depends on the application that you're building.

有不少人称自己是开发人员,不应该。话说回来。您发布的链接会详细介绍差异以及何时应该使用它们。最后,它取决于您正在构建的应用程序。

Quoted from the site you linked...

引用您链接的网站...

I recommend avoiding KeyValuePair entirely and using Tuple or custom classes (which would be similar to Tuple in performance).

我建议完全避免使用KeyValuePair并使用Tuple或自定义类(在性能上类似于Tuple)。

and

Tuple was faster in every test than KeyValuePair except in allocation performance. Therefore, if your program does any work beyond allocating the collections, it is a better idea to use Tuple instead of KeyValuePair.

除了分配性能之外,每次测试中的元组都比KeyValuePair更快。因此,如果您的程序除了分配集合之外还做任何工作,最好使用Tuple而不是KeyValuePair。

Illustrate the point I'm trying to make.

说明我正在努力做的事情。

For the big toolset that developers can use, when people tell me to never use something that's usually when I stop caring about their opinion. Nobody should tell you what to use without getting the context of the type of use it's going to get.

对于开发人员可以使用的大型工具集,当人们告诉我永远不会使用通常在我不再关心他们的意见时使用的东西。如果没有获得它将要获得的使用类型的上下文,没有人应该告诉你使用什么。

#3


1  

The KeyValuePair<> is a struct that represents a key and a value. When you enumerate a dictionary, the items are of the type KeyValuePair<> with the same generic types as the dictionary.

KeyValuePair <>是一个表示键和值的结构。枚举字典时,项目的类型为KeyValuePair <>,其类型与字典相同。

The Tuple<> classes are intended to represent multiple values, but there is no specific roles for the values like key or value. There are diffent Tuple<> classes for different number of values, from 1 up to 8 values.

Tuple <>类旨在表示多个值,但是没有像键或值这样的值的特定角色。对于不同数量的值,存在不同的Tuple <>类,从1到8个值。

The Tuple<> classes have names for the items like Item1, Item2, Item3 and so on. As those names doesn't tell you anything about what the items represent, you should preferably only use tuples where the meaning is obvious, for example quarters in a year.

Tuple <>类具有Item1,Item2,Item3等项目的名称。由于这些名称并未告诉您项目所代表的内容,因此您最好只使用明显含义的元组,例如一年中的季度。

#4


0  

They say don't use Tuple because Tuple has properties that don't have meaningful names: Item1, Item2, etc. Under some circumstances it's perfectly ok to use them. It is certainly less work to use a Tuple than to create your own every time.

他们说不要使用Tuple,因为Tuple具有没有有意义名称的属性:Item1,Item2等。在某些情况下,使用它们是完全可以的。使用元组肯定比每次创建自己更少。

One significant difference is that KeyValuePair is a struct and Tuples are classes. KeyValuePair has no relation to Tuples, really. It existed before Tuples to store keys and values in Dictionaries.

一个显着的区别是KeyValuePair是一个结构,而元组是类。 KeyValuePair与Tuples没有任何关系。它存在于Tuples之前,用于在Dictionaries中存储键和值。

You could argue about whether Tuples should have been implemented as value types instead. The ones you use most are the ones with few Type parameters and they would be better as value types. (You easily implement them yourself) Look here to see why they made certain decisions: http://msdn.microsoft.com/en-us/magazine/dd942829.aspx#id0400060

您可以争论是否应该将元组实现为值类型。你最常用的是Type参数很少的那些,它们会更好地作为值类型。 (您可以自己轻松实现它们)看看这里为什么他们做出了某些决定:http://msdn.microsoft.com/en-us/magazine/dd942829.aspx#id0400060

#1


0  

I think those programmers are saying not to use Tuples because on a public API, they are less effective.

我认为那些程序员说不使用元组,因为在公共API上,它们效率较低。

Lets say you have a tuple like

让我们说你有一个像元组

Tuple<int,string,string,int>

then how will you identify that what are these values later or what they are representing. I think thats why programmers say that. Like in this case the consumer has to either guess or look up documentation to know what you mean.

那么你将如何确定这些值以后是什么或它们代表什么。我想这就是程序员为什么这么说的原因。就像在这种情况下,消费者必须猜测或查找文档以了解您的意思。

The MSDN says:

MSDN说:

A tuple is a data structure that has a specific number and sequence of elements.

元组是具有特定数量和元素序列的数据结构。

Tuples are commonly used in four ways:

元组通常以四种方式使用:

  • To represent a single set of data. For example, a tuple can represent a database record, and its components can represent individual fields of the record.
  • 表示单组数据。例如,元组可以表示数据库记录,其组件可以表示记录的各个字段。
  • To provide easy access to, and manipulation of, a data set.
  • 提供对数据集的轻松访问和操作。
  • To return multiple values from a method without using out parameters (in C#) or ByRef parameters (in Visual Basic).
  • 从方法返回多个值而不使用out参数(在C#中)或ByRef参数(在Visual Basic中)。
  • To pass multiple values to a method through a single parameter. For example, the Thread.Start(Object) method has a single parameter that
    lets you supply one value to the method that the thread executes at
    startup time. If you supply a Tuple object as the method argument,
    you can supply the thread’s startup routine with three items of data.
  • 通过单个参数将多个值传递给方法。例如,Thread.Start(Object)方法有一个参数,允许您为线程在启动时执行的方法提供一个值。如果提供Tuple对象作为方法参数,则可以为线程的启动例程提供三项数据。

#2


3  

There are quite a few people that call themselves developers that should not. Having said that. The link that you posted goes into some detail about the differences and when you should use them. In the end it depends on the application that you're building.

有不少人称自己是开发人员,不应该。话说回来。您发布的链接会详细介绍差异以及何时应该使用它们。最后,它取决于您正在构建的应用程序。

Quoted from the site you linked...

引用您链接的网站...

I recommend avoiding KeyValuePair entirely and using Tuple or custom classes (which would be similar to Tuple in performance).

我建议完全避免使用KeyValuePair并使用Tuple或自定义类(在性能上类似于Tuple)。

and

Tuple was faster in every test than KeyValuePair except in allocation performance. Therefore, if your program does any work beyond allocating the collections, it is a better idea to use Tuple instead of KeyValuePair.

除了分配性能之外,每次测试中的元组都比KeyValuePair更快。因此,如果您的程序除了分配集合之外还做任何工作,最好使用Tuple而不是KeyValuePair。

Illustrate the point I'm trying to make.

说明我正在努力做的事情。

For the big toolset that developers can use, when people tell me to never use something that's usually when I stop caring about their opinion. Nobody should tell you what to use without getting the context of the type of use it's going to get.

对于开发人员可以使用的大型工具集,当人们告诉我永远不会使用通常在我不再关心他们的意见时使用的东西。如果没有获得它将要获得的使用类型的上下文,没有人应该告诉你使用什么。

#3


1  

The KeyValuePair<> is a struct that represents a key and a value. When you enumerate a dictionary, the items are of the type KeyValuePair<> with the same generic types as the dictionary.

KeyValuePair <>是一个表示键和值的结构。枚举字典时,项目的类型为KeyValuePair <>,其类型与字典相同。

The Tuple<> classes are intended to represent multiple values, but there is no specific roles for the values like key or value. There are diffent Tuple<> classes for different number of values, from 1 up to 8 values.

Tuple <>类旨在表示多个值,但是没有像键或值这样的值的特定角色。对于不同数量的值,存在不同的Tuple <>类,从1到8个值。

The Tuple<> classes have names for the items like Item1, Item2, Item3 and so on. As those names doesn't tell you anything about what the items represent, you should preferably only use tuples where the meaning is obvious, for example quarters in a year.

Tuple <>类具有Item1,Item2,Item3等项目的名称。由于这些名称并未告诉您项目所代表的内容,因此您最好只使用明显含义的元组,例如一年中的季度。

#4


0  

They say don't use Tuple because Tuple has properties that don't have meaningful names: Item1, Item2, etc. Under some circumstances it's perfectly ok to use them. It is certainly less work to use a Tuple than to create your own every time.

他们说不要使用Tuple,因为Tuple具有没有有意义名称的属性:Item1,Item2等。在某些情况下,使用它们是完全可以的。使用元组肯定比每次创建自己更少。

One significant difference is that KeyValuePair is a struct and Tuples are classes. KeyValuePair has no relation to Tuples, really. It existed before Tuples to store keys and values in Dictionaries.

一个显着的区别是KeyValuePair是一个结构,而元组是类。 KeyValuePair与Tuples没有任何关系。它存在于Tuples之前,用于在Dictionaries中存储键和值。

You could argue about whether Tuples should have been implemented as value types instead. The ones you use most are the ones with few Type parameters and they would be better as value types. (You easily implement them yourself) Look here to see why they made certain decisions: http://msdn.microsoft.com/en-us/magazine/dd942829.aspx#id0400060

您可以争论是否应该将元组实现为值类型。你最常用的是Type参数很少的那些,它们会更好地作为值类型。 (您可以自己轻松实现它们)看看这里为什么他们做出了某些决定:http://msdn.microsoft.com/en-us/magazine/dd942829.aspx#id0400060