다음을 통해 공유


컴파일러 오류 CS0266

암시적으로 'type1' 형식을 'type2' 형식으로 변환할 수 없습니다. 명시적 변환이 있습니다. 캐스트가 있는지 확인하세요.

이 오류는 코드에서 암시적으로 변환할 수 없는 두 형식 간에 변환하려고 하지만 명시적 변환을 사용할 수 있는 경우 발생합니다. 자세한 내용은 캐스팅 및 형식 변환을 참조하세요.

다음 코드는 CS0266을 생성하는 예제입니다.

// CS0266.cs
class MyClass
{
    public static void Main()
    {
        // You cannot implicitly convert a double to an integer.
        double d = 3.2;

        // The following line causes compiler error CS0266.
        int i1 = d;

        // However, you can resolve the error by using an explicit conversion.
        int i2 = (int)d;  

        // You cannot implicitly convert an object to a class type.
        object obj = new MyClass();

        // The following assignment statement causes error CS0266.
        MyClass myClass = obj;

        // You can resolve the error by using an explicit conversion.
        MyClass c = (MyClass)obj;

        // You cannot implicitly convert a base class object to a derived class type.
        MyClass mc = new MyClass();
        DerivedClass dc = new DerivedClass();

        // The following line causes compiler error CS0266.
        dc = mc;

        // You can resolve the error by using an explicit conversion.
        dc = (DerivedClass)mc;
    }  
}  
  
class DerivedClass : MyClass  
{  
}