Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the C# compiler mean when it prints "an explicit conversion exists"?

Tags:

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:

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):

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?

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 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.

The erroneous error then disappears again when you explicitly hit F6 to build.

like image 851
Wim Coenen Avatar asked Mar 26 '10 16:03

Wim Coenen


People also ask

Which is positive C or T?

One coloured line should be in the control line region (C), and another coloured line should be in the test line region (T). Two lines, one next to C and one next to T, even faint lines, show the test is positive.

What does the C and T mean on the lateral flow test?

This is what the test “window” looks like in the strip after 30 minutes. “C” stands for “control” – this is to make sure the test is working. “T” stands for “test” – this is where your sample result will appear. Image © https://www.gov.uk/ Negative result: one line next to C shows the test is negative.

Is C or T positive on Covid test?

For most positive tests, a reddish-purple line will appear in the Control (C) Zone and the Test (T) Zone; however, in cases where the viral load in the sample is very high, the line in the Control (C) Zone may not be present or may be very faint.

What is C and T in rapid test?

Positive Result: If you see two lines, Control (C) line and Test (T) line, this means COVID-19 was detected. If positive, please contact your doctor or local health department immediately and follow local guidelines for self-isolation. + 2. Ensure kit is at room temperature for at least 30 minutes prior to use.


1 Answers

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.

Thanks!

UPDATE: Apparently it does not reproduce on the command line, but is alleged to reproduce in 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.

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".

like image 131
Eric Lippert Avatar answered Oct 18 '22 15:10

Eric Lippert