编译器错误 CS0172
无法确定条件表达式的类型,因为“type1”和“type2”可相互隐式转换
在条件语句中,必须能够隐式转换 :
标记左右两侧的类型。 此外,不能有相互隐式转换;只需一次转换。
下面的示例生成 CS0172:
C#
// 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;
}
}