Is Using .NET 4.0 Tuples in my C# Code a Poor Design Decision?(在我的 C# 代码中使用 .NET 4.0 元组是一个糟糕的设计决策吗?)
问题描述
在 .net 中添加了 Tuple 类4,我一直在尝试决定在我的设计中使用它们是否是一个糟糕的选择.在我看来,Tuple 可以是一个快捷方式编写一个结果类(我相信还有其他用途).
With the addition of the Tuple class in .net 4, I have been trying to decide if using them in my design is a bad choice or not. The way I see it, a Tuple can be a shortcut to writing a result class (I am sure there are other uses too).
所以这个:
public class ResultType
{
public string StringValue { get; set; }
public int IntValue { get; set; }
}
public ResultType GetAClassedValue()
{
//..Do Some Stuff
ResultType result = new ResultType { StringValue = "A String", IntValue = 2 };
return result;
}
相当于这个:
public Tuple<string, int> GetATupledValue()
{
//...Do Some stuff
Tuple<string, int> result = new Tuple<string, int>("A String", 2);
return result;
}
因此,抛开我错过元组的可能性,是 元组 一个糟糕的设计选择?对我来说,它似乎不那么混乱,但不像自我记录和干净.这意味着对于 ResultType
类型,稍后将非常清楚类的每个部分的含义,但是您需要维护额外的代码.使用 Tuple
Item
代表什么,但您编写和维护的代码更少.
So setting aside the possibility that I am missing the point of Tuples, is the example with a Tuple a bad design choice? To me it seems like less clutter, but not as self documenting and clean. Meaning that with the type ResultType
, it is very clear later on what each part of the class means but you have extra code to maintain. With the Tuple<string, int>
you will need to look up and figure out what each Item
represents, but you write and maintain less code.
非常感谢您在此选择方面的任何经验.
Any experience you have had with this choice would be greatly appreciated.
推荐答案
如果您可以控制元组的创建和使用,那么元组非常棒 - 您可以维护上下文,这对于理解它们至关重要.
Tuples are great if you control both creating and using them - you can maintain context, which is essential to understanding them.
但是,在公共 API 上,它们的效率较低.消费者(不是您)必须猜测或查找文档,尤其是对于像 Tuple<int, int>
这样的东西.
On a public API, however, they are less effective. The consumer (not you) has to either guess or look up documentation, especially for things like Tuple<int, int>
.
我会将它们用于私有/内部成员,但将结果类用于公共/受保护成员.
I would use them for private/internal members, but use result classes for public/protected members.
这个答案有一些信息.
这篇关于在我的 C# 代码中使用 .NET 4.0 元组是一个糟糕的设计决策吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在我的 C# 代码中使用 .NET 4.0 元组是一个糟糕的设计决策吗?
基础教程推荐
- 将 XML 转换为通用列表 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- rabbitmq 的 REST API 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- 如何激活MC67中的红灯 2022-01-01