问题描述
随着.NET 4中加入元组类的,我一直试图决定是否将它们用在我的设计是一个不错的选择与否。我看到它的方式,一个元组可以是一个快捷方式写一个结果类(我相信还有其他用途也是如此)。
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
,这是很清楚的以后是什么类的手段的每个部分,但你有额外的code来维持。随着元组LT;字符串,INT&GT;
您将需要查找并弄清每个项目
重presents,但你编写和维护较少code。
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.
您有过这样的选择方面的经验将大大AP preciated。
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,但是,他们是不那么有效。消费者(不是你)有两种猜测或查找文档,尤其是对于像元组LT; INT,INT&GT;
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>
.
我会用他们的私有/内部成员,但使用结果类的公共/ protected成员。
I would use them for private/internal members, but use result classes for public/protected members.
This回答也有一些信息。
这篇关于在我的C#codeA糟糕的设计决定使用.NET 4.0元组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!