gpt4 book ai didi

c# - 使用 out 和可选参数的方法重载

转载 作者:行者123 更新时间:2023-11-30 21:57:46 25 4
gpt4 key购买 nike

为什么像这样的重载在 C# 中有效?

public string DisplayOverload(string a, string b, string c = "c")
{
return a + b + c;
}
public string DisplayOverload(string a, string b, out string c)
{
c = a + b;
return a + b;
}

虽然这不起作用

public string DisplayOverload(string a, string b, string c = "c")
{
return a + b + c;
}
public string DisplayOverload(string a, string b, string c)
{
return a + b + c;
}

最佳答案

outref视为方法签名的一部分。

来自 $3.6 Signatures and overloading ;

Note that any ref and out parameter modifiers (Section 10.5.1) are part of a signature. Thus, F(int) and F(ref int) are unique signatures.

你的第二个例子,coptional argument .即使您在没有此参数值的情况下调用,也会调用 3 个参数重载方法,但编译器无法知道调用哪一个。

更多信息,查看Eric Lippert's answer关于这个话题。

关于c# - 使用 out 和可选参数的方法重载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30527320/

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