c# - 使用 ref 或 out 参数编写 iron python 方法

标签 c# ironpython

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

private void GetTP(string name, out string ter, out int prov)
{
  ter = 2;
  prov = 1;
}

最佳答案

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

所以你不能直接将给定的代码翻译成 python,但你必须这样做:

def GetTP(name):
  return tuple([2, 1])

当你调用它时你必须做:

retTuple = GetTP(name)
ter = retTuple[0]
prov = retTuple[1]

这与在 IronPython 中调用包含 out/ref 参数的 C# 方法时的行为相同。

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

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

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

关于c# - 使用 ref 或 out 参数编写 iron python 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2857287/

相关文章:

python - IronPython 网络框架

python - 使用 python 设置背景图像

ironpython - 使用列名切换我行中的第一组值 (IronPython)

c# - 如何运行分配给模拟的事件处理程序?

c# - C# Console.WriteLine(double) 是否以完全精度写入此变量?

C# - 图像比较(快速)

python - 在 IronPython 中将复选框绑定(bind)到字符串

c# - 让 xpath 函数 ends-with() 工作而 contains() 工作正常时出现问题

c# - 在 C# 中如何检查数字是正数还是负数?

c# - 如何将非命名空间类型导入 IronPython?