コンパイラ エラー CS0173

'class1' と 'class2' の間に暗黙的変換がないため、条件式の型を決定できません。

クラス間の変換は、さまざまなクラスのオブジェクトと同じコードを使用する際に便利です。 しかしながら、連動する 2 つのクラスに相互変換や冗長的変換を使用することはできません。または暗黙的変換を使用しないことはできません。 class1class2 の型は別々に決定されます。より一般的な型は条件式の型として選択されます。 型の決定方法に関する詳細については、「Conditional operator cannot cast implicitly」 (条件演算子は暗黙的に型変換できない) を参照してください。

CS0173 を解決するには、変換の方向またはクラスに関係なく、class1class2 の暗黙的変換が 1 つだけ存在することを確認します。 詳細については、「ユーザー定義の変換演算子」と「組み込みの数値変換」を参照してください。

次の例ではコンパイラ エラー CS0173 が生成されます。

public class C {}

public class A
{
    // The following code defines an implicit conversion operator from
    // type C to type A.
    //public static implicit operator A(C c)
    //{
    //    A a = new A();
    //    a = c;
    //    return a;
    //}
}

public class MyClass
{
    public static void F(bool b)
    {
        A a = new A();
        C c = new C();

        // The following line causes CS0173 because there is no implicit
        // conversion from a to c or from c to a.
        object o = b ? a : c;

        // To resolve the error, you can cast a and c.
        // object o = b ? (object)a : (object)c;

        // Alternatively, you can add a conversion operator from class C to
        // class A, or from class A to class C, but not both.
    }

   public static void Main()
   {
      F(true);
   }
}
class M
{
    static int Main ()
    {
        int X = 1;
        // The following line causes CS0173.
        object o = (X == 0) ? null : null;
        return -1;
    }
}