Jun*_* HU 5 c# python function
我有一个python脚本:
def f():
a = None
b = None
return (a, b)
a, b = f()
Run Code Online (Sandbox Code Playgroud)
在python中实现多个返回值非常容易.现在我想在C#中实现相同的结果.我试过几种方法,比如return int []或KeyValuePair.但两种方式看起来并不优雅.我想知道一个令人兴奋的解 非常感谢.
使用元组类.
public Tuple<int,int> f()
{
Tuple<int,int> myTuple = new Tuple<int,int>(5,5);
return myTuple;
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,C# 不支持这一点。您可以获得的最接近的是使用out参数:
void f(out int a, out int b) {
a = 42;
b = 9;
}
int a, b;
f(out a, out b);
Run Code Online (Sandbox Code Playgroud)