C#错误(或功能) [英] C# Bug (or Feature)

查看:86
本文介绍了C#错误(或功能)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看看下面的示例C#代码:

Have a look at the following example C# code:

   课程计划
    {
       公共静态无效F(Func< object,object> a,谓词< object> b = null,字符串c = null)
        {
        }

       公共静态无效F(Action< object> a,谓词< object> b = null)
        {
        }

       公共静态空隙G(对象o)
        {
        }

       静态void Main(string [] args)
        {
            F(G,_ =>是). //第一次通话
            F(G); //第二个电话
        }
    }

    class Program
    {
        public static void F(Func<object, object> a, Predicate<object> b = null, string c = null)
        {
        }

        public static void F(Action<object> a, Predicate<object> b = null)
        {
        }

        public static void G(object o)
        {
        }

        static void Main(string[] args)
        {
            F(G, _ => true); // first call
            F(G); // second call
        }
    }

编译器将不会编译它,并且仅报告F的第二次调用的错误(歧义调用).
有趣的是,第一次调用被编译,并且正确"消息被编译.选择F版本.
如果删除字符串参数,则F的两个调用都是不明确的.

The compiler will not compile that and reports a bug (ambiguous call) for the second call of F only.
Interesting enough the first call compiles and the "correct" version of F is choosen.
If you remove the string-parameter both calls of F are ambiguous.

我首先以为ReSharper有一个错误,但是ReSharper总是在所有版本中发现错误.

I first thought that ReSharper has a bug, but ReSharper always finds errors on all versions.

我的印象:这至少是前后矛盾还是我错了?

My impression: this is at least inconsistent or am I wrong?

推荐答案

绝对是一个错误:

Definitely a bug:

1.编译器应始终能够将"void"方法与"Action"参数进行匹配.

1. The compiler should be able to always match up a 'void' method with the 'Action' parameter.

2.第一种方法中的可选字符串参数在这里不应该成为一个因素.

2. The optional string parameter in the first method shouldn't be a factor here.

将其报告给Microsoft-我相信他们会在几年后使用它.

Report it to Microsoft - I'm sure they'll get to it in a few years.


这篇关于C#错误(或功能)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆