加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 23:39:49 所属栏目:百科 来源:网络整理
导读:简单地说,你说有两种方法: public static void WriteMessage() { Console.Write("Empty Parameter Function");}public static void WriteMessage(string data = "Some Data"){ Console.Write("Optional Parameter Function");} 为什么如果在没有任何参数的
简单地说,你说有两种方法:

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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读