Better naming in Tuple classes than quot;Item1quot;, quot;Item2quot;(元组类中的命名比“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"更好.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:元组类中的命名比“Item1"、“Item2"更好.


- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- 输入按键事件处理程序 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01