Export (0) Print
Expand All
Expand Minimize

Compiler Error CS0173

Type of conditional expression cannot be determined because there is no implicit conversion between 'class1' and 'class2'

Conversions between classes are useful when you want objects of different classes to work with the same code. However, two classes that work together cannot have mutual and redundant conversions, or no implicit conversions. The types of class1 and class2 are determined independently, and the more general type is selected as the type of the conditional expression. For more information about how types are determined, see Conditional operator cannot cast implicitly.

To resolve CS0173, verify that there is one and only one implicit conversion between class1 and class2, regardless of which direction the conversion is in and regardless of which class the conversion is in. For more information, see Implicit Numeric Conversions Table (C# Reference) and Conversion Operators (C# Programming Guide).

The following sample generates CS0173:

// CS0173.cs
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);
   }
}

The following code does not produce CS0173 in Visual Studio 2005, but does in later versions.

//cs0173_2.cs
class M
{
    static int Main ()
    {
        int X = 1;
        // The following line causes CS0173 in Visual Studio 2005.
        object o = (X == 0) ? null : null;
        return -1;
    }
}
Show:
© 2014 Microsoft