元组旨在解决什么要求?

What requirement was the tuple designed to solve?(元组旨在解决什么要求?)

本文介绍了元组旨在解决什么要求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究元组的新 C# 功能.我很好奇,这个元组旨在解决什么问题?

I'm looking at the new C# feature of tuples. I'm curious, what problem was the tuple designed to solve?

您在应用中使用元组做什么?

What have you used tuples for in your apps?

更新

感谢到目前为止的答案,让我看看我是否有正确的想法.元组的一个很好的例子是坐标.这看起来对吗?

Thanks for the answers thus far, let me see if I have things straight in my mind. A good example of a tuple has been pointed out as coordinates. Does this look right?

var coords = Tuple.Create(geoLat,geoLong);

然后像这样使用元组:

var myLatlng = new google.maps.LatLng("+ coords.Item1 + ", "+ coords.Item2 + ");

对吗?

推荐答案

在编写程序时,希望在逻辑上将一组值组合在一起,这些值没有足够的共性来证明创建一个类是合理的.

When writing programs it is extremely common to want to logically group together a set of values which do not have sufficient commonality to justify making a class.

许多编程语言允许您在逻辑上将一组原本不相关的值组合在一起,而无需仅以一种方式创建类型:

Many programming languages allow you to logically group together a set of otherwise unrelated values without creating a type in only one way:

void M(int foo, string bar, double blah)

从逻辑上讲,这与采用一个参数的方法 M 完全相同,该参数是 int、string、double 的 3 元组.但我希望你不会真的做出:

Logically this is exactly the same as a method M that takes one argument which is a 3-tuple of int, string, double. But I hope you would not actually make:

class MArguments
{
   public int Foo { get; private set; } 
   ... etc

除非 MArguments 在业务逻辑中有其他含义.

unless MArguments had some other meaning in the business logic.

以比类更轻量级的结构将一堆原本不相关的数据组合在一起"的概念在很多很多地方都很有用,而不仅仅是方法的形式参数列表.当一个方法有两个东西要返回时,或者当你想从两个数据而不是一个数据中键入一个字典时,它很有用,等等.

The concept of "group together a bunch of otherwise unrelated data in some structure that is more lightweight than a class" is useful in many, many places, not just for formal parameter lists of methods. It's useful when a method has two things to return, or when you want to key a dictionary off of two data rather than one, and so on.

像 F# 这样原生支持元组类型的语言为其用户提供了极大的灵活性;它们是一组非常有用的数据类型.BCL 团队决定与 F# 团队合作,为框架标准化一种元组类型,以便每种语言都可以从中受益.

Languages like F# which support tuple types natively provide a great deal of flexibility to their users; they are an extremely useful set of data types. The BCL team decided to work with the F# team to standardize on one tuple type for the framework so that every language could benefit from them.

但是,目前 C# 中的元组不支持 语言.元组只是与任何其他框架类一样的另一种数据类型.他们没有什么特别的.我们正在考虑在假设的 C# 未来版本中添加对元组的更好支持.如果有人对您希望看到的涉及元组的哪种功能有任何想法,我很乐意将它们传递给设计团队.现实场景比理论思考更有说服力.

However, there is at this point no language support for tuples in C#. Tuples are just another data type like any other framework class; there's nothing special about them. We are considering adding better support for tuples in hypothetical future versions of C#. If anyone has any thoughts on what sort of features involving tuples you'd like to see, I'd be happy to pass them along to the design team. Realistic scenarios are more convincing than theoretical musings.

这篇关于元组旨在解决什么要求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:元组旨在解决什么要求?

基础教程推荐