什么是C#编译器的意思是当它打印"存在显式转换"? [英] What does the C# compiler mean when it prints "an explicit conversion exists"?

查看:211
本文介绍了什么是C#编译器的意思是当它打印"存在显式转换"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我做一个空的测试类:

If I make an empty test class:

public class Foo
{
}

和我尝试用这句话来编译代码:

And I try to compile code with this statement:

Foo foo = "test";



然后,我得到这个错误预期:

Then I get this error as expected:

无法隐式转换类型'字符串'到'ConsoleApplication1.Foo

Cannot implicitly convert type 'string' to 'ConsoleApplication1.Foo'

不过,如果我改变从类声明的接口,错误修改本(重点煤矿):

However, if I change the declaration of Foo from class to interface, the error changes to this (emphasis mine):

无法隐式转换类型
'串'到'ConsoleApplication1.Foo。
的显式转换存在(你
缺少强制转换?)

Cannot implicitly convert type 'string' to 'ConsoleApplication1.Foo'. An explicit conversion exists (are you missing a cast?)

这是什么显式转换这是应该存在的。

What is this "explicit conversion" which is supposed to exist?

更新:这个问题是一个比较微妙的,比我最初以为。要重现它,把这个代码在一个新的控制台应用程序在Visual Studio 2008中:

update: the issue is a bit more subtle than I initially thought. To reproduce it, put this code in a new console application in Visual Studio 2008:

namespace ConsoleApplication1
{
    class Foo
    {
    }

    interface IFoo
    {
    }


   class Program
   {
      static void Main(string[] args)
      {
         Foo b = "hello";
      }
   }
}



Visual Studio将自动显示在这一点上正确的错误(您生成代码之前)。现在,将我把富到的IFoo和等待几秒钟,无需建立。 显式的转换不存在错误的版本,现在将错误输出窗口和工具提示分配错误自动出现。

Visual studio will automatically show the correct error at this point (before you build the code). Now insert the "I" to turn "Foo" into "IFoo" and wait a few seconds without building. The "explicit conversion exists" version of the error will now appear automatically in the error output window and in the tool tip for the assignment error.

错误的错误,那么<强>当你明确地击中F6建设再次消失

推荐答案

我无法重现的报道行为。如果事实上确实重现,这是一个错误。有一个从字符串到任何用户自定义界面没有明确的转换。

I am unable to reproduce the reported behaviour. If it does in fact reproduce, that's a bug. There is no explicit conversion from string to any user-defined interface.

请更新您使用的编译器和一个小程序,重新产生问题的版本号的问题,我会得到一个错误输入到错误。数据库

Please update the question with the version number of the compiler you're using and a small program that reproduces the problem, and I'll get a bug entered into the bug database.

感谢

更新:显然,这不会重现在命令行上,但被指重现VS2008。

UPDATE: Apparently it does not reproduce on the command line, but is alleged to reproduce in VS2008.

我无法重现它在VS2010的RC版本,因此,如果这实际上在VS2008中的错误,它可能得到修复。我没有VS2008得心应手现在就可惜测试的安装。

I am unable to reproduce it in the RC build of VS2010, so if this was in fact a bug in VS2008, it's probably been fixed. I don't have an installation of VS2008 handy right now to test unfortunately.

无论如何,如果你看到了诊断,然后赔率是非常好的那简直就是一个bug在错误报告中的语义分析启发。显然,从字符串的IFoo没有明确的转换。

Regardless, if you're seeing that diagnostic then odds are very good it is simply a bug in the error reporting heuristics in the semantic analyzer. Clearly there is no explicit conversion from string to IFoo.

有任何的启封的类型的接口类型的显式转换,因为有可能是一个派生类型,它实现的接口。但字符串是密封的,所以错误应该仅仅是不转换。

There is an explicit conversion from any unsealed type to any interface type because there could be a derived type which implements the interface. But string is sealed, so the error should simply be "no conversion".

这篇关于什么是C#编译器的意思是当它打印&QUOT;存在显式转换&QUOT;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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