何时使用:C# 7.0 中的元组与类

When to use: Tuple vs Class in C# 7.0(何时使用:C# 7.0 中的元组与类)

本文介绍了何时使用:C# 7.0 中的元组与类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用元组之前,我曾经创建一个类及其变量,然后从这个类创建对象,并使该对象成为某些函数的返回类型.

Before Tuples, I used to create a class and its variables, then create object from this class and make that object the return type for some functions.

现在,使用元组,我可以做同样的事情,在 C# 7.0 中,我们可以为元组属性分配易于理解的名称(在此之前,它是 item1item2等.)

Now, with tuples, I can do the same thing, and in C# 7.0 we can assign understandable names for tuple properties (before this, it was item1, item2, etc..)

所以现在我想知道,我什么时候使用元组,什么时候在 C# 7.0 中创建一个类?

So now I am wondering, when do I use tuple and when do I create a class in C# 7.0?

推荐答案

由于这个答案在这里引起了一些人的困惑,我应该澄清一下——根据问题——这里所有对元组"的引用都是指 ValueTuple 类型和 C# 7 的新元组语法糖功能,绝不引用旧的 System.Tuple 引用类型.

As this answer is causing some confusion amongst some folk here, I should clarify that - as per the question - all references to "tuple" here refer to the ValueTuple type and new tuple syntactic sugar features of C# 7 and in no way refer to the old System.Tuple reference types.

所以现在我想知道,什么时候应该使用元组,什么时候应该在 c# 7.0 中创建一个类?

So now I am wondering, when Should I use tuples and when Should I create a class in c# 7.0?

只有你才能真正回答这个问题,因为它真的取决于你的代码.

Only you can really answer that question as it really depends on your code.

但是,您可以遵循一些指导方针和规则来指导您在它们之间进行选择:

However, there are guidelines and rules you can follow in guiding you in choosing between them:

大多数时候,这应该不是问题.但是,如果您正在传递大型结构的元组,这可能会对性能产生影响.不过,可以使用 Ref locals/returns 来解决这些性能问题.

Most of the time, this should not be an issue. However, if you are passing around tuples of large structs, this might have an impact on performance. Ref locals/returns can be used to work around these performance issues, though.

另外,由于它们是值,远程修改副本不会更改原始副本.这是一件好事,但可能会吸引一些人.

Additionally, because they are values, modifying a copy remotely will not change the original copy. This is a good thing, but could catch some folk out.

给元素的名称由编译器使用,并且(在大多数情况下)在运行时不可用.这意味着不能使用反射来发现它们的名称;它们不能动态访问,也不能在 razor 视图中使用.

The names given to elements are used by the compiler and (in most cases) are not available at run-time. This means that reflection cannot be used to discover their names; they cannot be accessed dynamically and they cannot be used in razor views.

这也是 API 的一个重要考虑因素.从方法返回的元组是关于编译后名称可发现性规则的例外.编译器将属性添加到保存元组名称信息的方法中.这意味着您可以安全地从一个程序集中的公共方法返回一个元组,并在另一个程序集中访问它的名称.

Also this is an important consideration with APIs. A tuple returned from a method is the exception to the rule regarding after-compilation name discoverability. The compiler adds attributes to the method that hold information on the tuple names. This means you can safely return a tuple from a public method in one assembly and access its names in another.

元组比类型更容易编写,因为它们不那么冗长,并且声明可以内联"(即在使用点声明).例如,这在声明返回多个值的方法时非常有效.

Tuples are much simpler to write than types as they are less verbose and the declaration can be "inlined" (ie declared at the point of use). This works well when declaring a method that returns multiple values, for example.

但是,因为它们是在使用时声明的,如果你有 MethodA 调用 MethodB 调用 MethodC 并且每个返回一个元组,您需要在每个阶段重新定义元组.没有(yet)一种创建元组别名并重新在多种方法中使用它.

However, because they are declared at the point of use, if you have MethodA that calls MethodB that calls MethodC and each returns a tuple, you'll need to redefine the tuple at every stage. There isn't (yet) a way of creating an alias of a tuple and re-using it across multiple methods.

对于您可能考虑使用元组的任何情况:只需问自己一个问题:元组是否会简化此处的代码".如果答案是是",则使用一个.这最终是使用元组还是自定义类的首要考虑因素.

For any situation where you might consider using a tuple: simply ask yourself the question: "will a tuple simplify the code here". If the answer is "yes", then use one. And that ultimately is the primary consideration over whether to use a tuple or a custom class.

这篇关于何时使用:C# 7.0 中的元组与类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:何时使用:C# 7.0 中的元组与类

基础教程推荐