何时使用:C#7.0中的Tuple vs Class [英] When to use: Tuple vs Class in C# 7.0

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

问题描述

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

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中,我们可以为元组属性分配易于理解的名称(在此之前,它是 item1 item2 等.)

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 should I use tuple and when should I create a class in C# 7.0?

推荐答案

由于此答案在此处引起某些人的困惑,因此我应该澄清-根据问题-此处对元组"的所有引用均指 ValueTuple 类型和C#7的新tuple语法糖功能,决不能引用旧的 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.

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

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 ,并且每个方法都返回了一个元组,您需要在每个阶段重新定义元组.还没有(> )创建元组别名并重新在多种方法中使用它.

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中的Tuple vs Class的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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