元组类中的命名比“Item1"、“Item2"更好. [英] Better naming in Tuple classes than "Item1", "Item2"

查看:25
本文介绍了元组类中的命名比“Item1"、“Item2"更好.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法使用 Tuple 类,但提供其中项目的名称?

Is there a way to use a Tuple class, but supply the names of the items in it?

例如:

public Tuple<int, int, int int> GetOrderRelatedIds()

返回 OrderGroupId、OrderTypeId、OrderSubTypeId 和 OrderRequirementId 的 ID.

That returns the ids for OrderGroupId, OrderTypeId, OrderSubTypeId and OrderRequirementId.

最好让我的方法的用户知道哪个是哪个.(调用方法时,结果是result.Item1,result.Item2,result.Item3,result.Item4.不清楚哪个是哪个.)

It would be nice to let the users of my method know which is which. (When you call the method, the results are result.Item1, result.Item2, result.Item3, result.Item4. It is not clear which one is which.)

(我知道我可以创建一个类来保存所有这些 Id,但是这些 Id 已经有它们自己的类,并且为这个方法的返回值创建一个类似乎很愚蠢.)

(I know I could just create a class to hold all these Ids, but it these Ids already have their own classes they live in and making a class for this one method's return value seems silly.)

推荐答案

在 C# 7.0 (Visual Studio 2017) 中有一个新的结构可以做到这一点:

In C# 7.0 (Visual Studio 2017) there is a new construction to do that:

(string first, string middle, string last) LookupName(long id)

这篇关于元组类中的命名比“Item1"、“Item2"更好.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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