c# - 如何从一个方法返回多个值?

标签 c#

<分区>

我想从 C# 中的方法返回年龄值,如下所示

private int UpdateAges( int age1,  int age2,  int age3,  int age4)
{
    age1++;
    age2++;
    age3++;
    age4++;
    return (age1, age2, age3, age4);
}

我可以像下面这样使用它

MessageBox.Show(UpdateAges(45, 30, 45, 30).ToString());

最佳答案

试试这个:

private Tuple<int, int, int, int> UpdateAges( int age1,  int age2,  int age3,  int age4)
{
    age1++;
    age2++;
    age3++;
    age4++;

    return Tuple.Create(age1, age2, age3, age4);
}

另请参阅:关于 C# 7 new included Tuples syntax

关于c# - 如何从一个方法返回多个值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52529530/

相关文章:

c# - 同一对象上的多个锁是否存在死锁情况?

c# - 带圆角的数据网格模板

c# - 有没有什么工具可以分析c#程序中变量之间的依赖关系?

c# - Xamarin.iOS 上的回声消除 (AEC)

C# 文件为空,创建实例时崩溃。

c# - 在 ASP.NET 中,如何读取位于我的页面所在的同一文件夹中的文件?

c# - 初始化 C# IntPtr 以接受来自非托管 C++ DLL 的数据?

C# 等价于 C++ 回调

c# - 强制字符串插值始终遵循 CultureInfo.InvariantCulture

c# - 在 .NET 中截取屏幕截图后出现运行时错误