Compiler Error CS0173
Visual Studio 2008
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 will work together cannot have mutual and redundant conversions.
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 {}
public class MyClass
{
public static void F(bool b)
{
A a = new A();
C c = new C();
object o = b ? a : c; // CS0173
}
public static void Main()
{
F(true);
}
}
The following code produces CS0173 in Microsoft Visual Studio 2008 but not in Visual Studio 2005.
//cs0173_2.cs
class M
{
static int Main ()
{
int X = 1;
object o = (X == 0) ? null : null; //CS0173
return -1;
}
}
If this happens when working with generics...
If you happen to have something like this:
int? a = (string.IsNullOrEmpty(myString)) ? Convert.ToInt32(myString) : null;
Just cast Convert.ToInt32 into int?:
int? a = (string.IsNullOrEmpty(myString)) ? (int?)Convert.ToInt32(myString) : null;
- 12/13/2010
- Alexei Humeniy
How to fix this error
The compiler error is due to there being no implicit cast between a and c, so if both are first cast to an object, the compiler can now convert between both as they are now both of the same type.
In the example CS0173.cs, cast both a and c to be object and the error is gone.
object o = b ? (object)a : (object)c; // No more CS0173!
- 6/16/2009
- Hawky