Compiler Error CS0172
Type of conditional expression cannot be determined because 'type1' and 'type2' implicitly convert to one another
In a conditional statement, you must be able to implicitly convert the types on either side of the :
token. Also, there cannot be mutual implicit conversions; you only need one conversion.
The following sample generates CS0172:
// CS0172.cs
public class Square
{
public class Circle
{
public static implicit operator Circle(Square aa)
{
return null;
}
public static implicit operator Square(Circle aa)
// using explicit resolves this error
// public static explicit operator Square(Circle aa)
{
return null;
}
}
public static void Main()
{
Circle aa = new Circle();
Square ii = new Square();
object o = (1 == 1) ? aa : ii; // CS0172
// the following cast would resolve this error
// (1 == 1) ? aa : (Circle)ii;
}
}
See also
Collaborate with us on GitHub
The source for this content can be found on GitHub, where you can also create and review issues and pull requests. For more information, see our contributor guide.
Feedback
https://aka.ms/ContentUserFeedback.
Coming soon: Throughout 2024 we will be phasing out GitHub Issues as the feedback mechanism for content and replacing it with a new feedback system. For more information see:Submit and view feedback for