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

If I create an empty test class:

public class Foo { } 

And I'm trying 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):

It is not possible to implicitly convert the type 'string' to 'ConsoleApplication1.Foo'. An explicit conversion exists (are you missing a listing?)

What is this โ€œexplicit transformation" that must exist?

update : the problem is a bit more complicated than I originally thought. To play 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 creating the code). Now insert โ€œIโ€ to turn โ€œFooโ€ into โ€œIFooโ€ and wait a few seconds without creating . An "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 error error then disappears again when you explicitly press F6 to build .

+8
c #
source share
6 answers

I cannot reproduce the reported behavior. If it really reproduces, this is a mistake. There is no explicit conversion from string to any user interface.

Please update the question with the version number of the compiler used and a small program that reproduces the problem, and I will get an error entered into the error database.

Thanks!

UPDATE: It does not appear to play on the command line, but is allegedly played in VS2008.

I can't play it in the VS2010 RC build, so if this is actually a bug in VS2008, it probably has been fixed. I don't have VS2008 installation right now to check, unfortunately.

Regardless of if you see this diagnostic, the chances are very good, itโ€™s just a mistake in the heuristic of error messages in the semantic analyzer. Obviously, there is no explicit conversion from string to IFoo.

There is an explicit conversion of any unsealed type to any type of interface, since there may be a derived type that implements the interface. But the string is sealed, so the error should just be "no conversion".

+10
source share

I reproduced this behavior. Reproduced.

Microsoft Visual Studio 2008

Version 9.0.30729.1 SP

Microsoft .NET Framework

Version 3.5 SP1

Installed Edition: Professional

+5
source share

Shamelessly torn from MSDN - Compiler Error CS0266 and MSDN - Vivid (link to C #) .

This error occurs if you have code that tries to convert two types that cannot be implicitly converted, for example, when assigning a base type to a derived type that lacks an explicit cast.

Explicit keyword Explicit declares a user-defined type conversion operator to be called by the application. For example, this operator converts from the Fahrenheit class to the Celsius class:

 // Must be defined inside a class called Farenheit: public static explicit operator Celsius(Farenheit f) { return new Celsius((5.0f/9.0f)*(f.degrees-32)); } 

This conversion operator can be called as follows:

 Farenheit f = new Farenheit(100.0f); Celsius c = (Celsius)f; 
+1
source share

Unable to reproduce this. CS0029 only has

Cannot implicitly convert type 'type' to 'type' "

CS0266 , however, says

It is not possible to implicitly convert type 'type1' to 'type2'. Explicit conversion exists (are you skipping listing?)

but with Foo being an empty class / interface, this is not possible, IMO.

0
source share

This error would occur if you wrote:

 class Foo:IFoo { } interface IFoo { } static void Main(string[] args) { IFoo i = new Foo(); Foo f = i; // here this message would occur, since IFoo *can* Convert to Foo (Since Foo implements IFoo), but it must be casted explicitly } 
0
source share

Yes, there is no explicit way to convert between Foo and string. However, if you want to use this syntax, Foo foo = "Hello World" as a shorthand you can. This is accomplished using the implicit operator, as defined here.

It allows you to perform these types of conversions implicitly (hence the name).

To accomplish this type of facade, here is how you do it:

 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ImplicitOperatorTest { class Foo { private string foo; public Foo(string foo) { this.foo = foo; } public static implicit operator string(Foo foo) { return foo; } public static implicit operator Foo(string foo) { return new Foo(foo); } } interface IFoo { } class Program { static void Main(string[] args) { Foo b = "hello"; } } } 
0
source share

All Articles