次の方法で共有


コンパイラ エラー CS0266

型 'type1' を型 'type2' に暗黙的に変換できません。 明示的な変換が存在します (型変換を忘れていませんか)。

このエラーは、暗黙的に変換できない 2 つの型間の変換をコードが試行したときに (ただし、明示的に変換できるときに) 発生します。 詳細については、「キャストと型変換」を参照してください。

次のコードは、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  
{  
}