Udostępnij za pośrednictwem


Compiler Error CS0266

Cannot implicitly convert type 'type1' to 'type2'. An explicit conversion exists (are you missing a cast?)

This error occurs if you have code that tries to convert two types that cannot be implicitly converted, such as in an assignment of a base type to a derived type that is missing an explicit cast. For more information, see Conversion Operators (C# Programming Guide).

The following sample generates CS0266:

// CS0266.cs
class MyClass
{
    public static void Main()
    {
        object obj = "MyString";
        // Cannot implicitly convert 'object' to 'MyClass'
        MyClass myClass = obj;  // CS0266
        // Try this line instead
        // MyClass c = ( MyClass )obj;
    }
}