是否有一种方法可以使用Tuple类,但在其中提供项目的名称?
例如:
public Tuple<int, int, int int> GetOrderRelatedIds()
它返回OrderGroupId、OrderTypeId、OrderSubTypeId和OrderRequirementId的id。
让我的方法的用户知道哪个是哪个就好了。(当您调用该方法时,结果为result。Item1,结果。第二条,结果。Item3 result.Item4。不清楚哪个是哪个。)
(我知道我可以创建一个类来保存所有这些id,但这些id已经有自己的类,它们生活在其中,为这个方法的返回值创建一个类似乎很愚蠢。)
(double, int) t1 = (4.5, 3);
Console.WriteLine($"Tuple with elements {t1.Item1} and {t1.Item2}.");
// Output:
// Tuple with elements 4.5 and 3.
(double Sum, int Count) t2 = (4.5, 3);
Console.WriteLine($"Sum of {t2.Count} elements is {t2.Sum}.");
// Output:
// Sum of 3 elements is 4.5.
来自Docs: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/value-tuples
到今天为止,就是这么简单。而不是使用Tuple关键字
public Tuple<int, int, int int> GetOrderRelatedIds()
用这个。
public (int alpha, int beta, int candor) GetOrderRelatedIds()
得到这样的值。
var a = GetOrderRelatedIds();
var c = a.alpha;
我想我会创建一个类,但另一种选择是输出参数。
public void GetOrderRelatedIds(out int OrderGroupId, out int OrderTypeId, out int OrderSubTypeId, out int OrderRequirementId)
因为你的元组只包含整数,你可以用Dictionary<string,int>来表示它
var orderIds = new Dictionary<string, int> {
{"OrderGroupId", 1},
{"OrderTypeId", 2},
{"OrderSubTypeId", 3},
{"OrderRequirementId", 4}.
};
但我也不建议这样做。