gpt4 book ai didi

c# - 可选参数与空参数方法和重载的构造函数

转载 作者:行者123 更新时间:2023-11-30 14:06:50 25 4
gpt4 key购买 nike

简单来说,假设你有两种方法:

public static void WriteMessage() 
{
Console.Write("Empty Parameter Function");
}



public static void WriteMessage(string data = "Some Data")
{
Console.Write("Optional Parameter Function");
}

为什么如果不带任何参数调用 WriteMessage 函数,它会运行“空参数函数”?我理解方法重载,但为什么可选参数函数不运行,就好像空参数函数不存在一样,它会运行?

最佳答案

关于过载解决方案的以下几点,来自MSDN , 解释了这个决定:

If two candidates are judged to be equally good, preference goes to a candidate that does not have optional parameters for which arguments were omitted in the call. This is a consequence of a general preference in overload resolution for candidates that have fewer parameters.

关于c# - 可选参数与空参数方法和重载的构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45059990/

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