.NET 的未来版本会支持 C# 中的元组吗? [英] Will a future version of .NET support tuples in C#?

查看:26
本文介绍了.NET 的未来版本会支持 C# 中的元组吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

.Net 3.5 不支持元组.太糟糕了,但不确定.net 的未来版本是否支持元组?

.Net 3.5 doesn't support tuples. Too bad, But not sure whether the future version of .net will support tuples or not?

推荐答案

我刚刚阅读了 MSDN 杂志上的这篇文章:构建元组

I've just read this article from the MSDN Magazine: Building Tuple

以下是摘录:

即将发布的 Microsoft 4.0 版本.NET Framework 引入了一种新类型称为 System.Tuple.System.Tuple 是一个固定大小的集合异构类型的数据.

The upcoming 4.0 release of Microsoft .NET Framework introduces a new type called System.Tuple. System.Tuple is a fixed-size collection of heterogeneously typed data.    

像数组一样,元组有一个固定的一旦有就不能改变的大小被创建.与数组不同,每个元组中的元素可能不同类型,并且一个元组能够保证每个元素的强类型.

Like an array, a tuple has a fixed size that can't be changed once it has been created. Unlike an array, each element in a tuple may be a different type, and a tuple is able to guarantee strong typing for each element.

已经有一个例子元组在 Microsoft 周围浮动.NET 框架,在System.Collections.Generic 命名空间:键值对.虽然 KeyValuePair 可以被认为是相同的作为元组,因为它们都是持有两种东西的类型,KeyValuePair 感觉不同于元组,因为它唤起了一种关系它存储的两个值之间(和有充分的理由,因为它支持字典类).

There is already one example of a tuple floating around the Microsoft .NET Framework, in the System.Collections.Generic namespace: KeyValuePair. While KeyValuePair can be thought of as the same as Tuple, since they are both types that hold two things, KeyValuePair feels different from Tuple because it evokes a relationship between the two values it stores (and with good reason, as it supports the Dictionary class).

此外,元组可以是任意的大小,而 KeyValuePair 仅持有两件事:一个键和一个值.

Furthermore, tuples can be arbitrarily sized, whereas KeyValuePair holds only two things: a key and a value.

<小时>

虽然某些语言(如 F#)对元组有特殊的语法,但您可以使用任何语言的新通用元组类型.回顾第一个例子,我们可以看到虽然有用,但元组在没有元组语法的语言中可能过于冗长:


While some languages like F# have special syntax for tuples, you can use the new common tuple type from any language. Revisiting the first example, we can see that while useful, tuples can be overly verbose in languages without syntax for a tuple:

class Program {
    static void Main(string[] args) {
        Tuple<string, int> t = new Tuple<string, int>("Hello", 4);
        PrintStringAndInt(t.Item1, t.Item2);
    }
    static void PrintStringAndInt(string s, int i) {
        Console.WriteLine("{0} {1}", s, i);
    }
}

使用 C# 3.0 中的 var 关键字,我们可以删除元组变量上的类型签名,从而使代码更具可读性.

Using the var keyword from C# 3.0, we can remove the type signature on the tuple variable, which allows for somewhat more readable code.

var t = new Tuple<string, int>("Hello", 4);

我们还在静态 Tuple 类中添加了一些工厂方法,这使得在支持类型推断的语言(如 C#)中构建元组变得更加容易.

We've also added some factory methods to a static Tuple class which makes it easier to build tuples in a language that supports type inference, like C#.

var t = Tuple.Create("Hello", 4);

这篇关于.NET 的未来版本会支持 C# 中的元组吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆