在不同的地方打印元组元素



有没有办法打印元组中的单个元素?尝试打印类似"最大值为" + max + "的内容,它位于" + pos + "位置。

public Tuple<int, int> find_oldie(int[] x)
{
int max = x.Max();
int pos = Array.IndexOf(x, max);
return Tuple.Create(max, pos);
}

您可以使用Value.Tuple返回Tuple的成员named

public (int Max, int Pos) find_oldie(int[] x)
{
int max = x.Max();
int pos = Array.IndexOf(x, max);
return (max, pos);
}

@LasseV.Karlsen已经详细解释了Value.Tuple和使用方法,但我想补充一点,deconstruct可以用来创建一个变量(它将具有命名成员(所以可以调用上面的函数作为:

var val = ReturnNamedTupple();
Console.WriteLine($"The max value is {val.Max} and it is located in the {val.Pos}");

最新更新