用ref或out参数编写铁python方法

aro*_*ron 8 c# ironpython

我需要将以下C#方法转换为相同的IronPhyton方法

private void GetTP(string name, out string ter, out int prov)
{
  ter = 2;
  prov = 1;
}
Run Code Online (Sandbox Code Playgroud)

dig*_*All 6

在python中(因此在IronPython中)你不能改变一个不可变的参数(比如字符串)

所以你不能直接将给定的代码转换为python,但是你必须做类似的事情:

def GetTP(name):
  return tuple([2, 1])
Run Code Online (Sandbox Code Playgroud)

当你打电话时,你必须做:

retTuple = GetTP(name)
ter = retTuple[0]
prov = retTuple[1]
Run Code Online (Sandbox Code Playgroud)

在IronPython中,您调用包含out/ref参数的C#方法时的行为相同.

事实上,在这种情况下,IronPython返回一个out/ref参数的元组,如果有一个返回值是元组中的第一个.

编辑:实际上可以用out/ref参数覆盖一个方法,看看这里:

http://ironpython.net/documentation/dotnet/dotnet.html#methods-with-ref-or-out-parameters