gpt4 book ai didi

c# - 是否有技术原因要求调用方使用 "out"和 "ref"关键字?

转载 作者:太空狗 更新时间:2023-10-29 20:44:14 26 4
gpt4 key购买 nike

当调用带有refout 参数的方法时,您必须在调用该方法时指定适当的关键字。我从样式和代码质量的角度理解这一点(例如 here 所解释的),但我很好奇是否还需要在调用方中指定关键字。

例如:

static void Main()
{
int y = 0;
Increment(ref y); // Is there any technical reason to include ref here?
}

static void Increment(ref int x)
{
x++;
}

最佳答案

我能想到的唯一技术原因是重载解析:你可以

static void Increment(ref int x)

还有

static void Increment(int x)

This is allowed ;如果调用中没有 ref,编译器将无法区分它们。

关于c# - 是否有技术原因要求调用方使用 "out"和 "ref"关键字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16096364/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com