編譯器錯誤 CS0172
更新:2007 年 11 月
錯誤訊息
無法確認條件運算式的型別,因為 'type1' 和 'type2' 彼此會隱含轉換
在條件陳述式中,您必須能夠轉換位於 : 運算子任一邊的型別。此外,無法有相互轉換常式,因為您只需要一種轉換。如需詳細資訊,請參閱轉換運算子 (C# 程式設計手冊)。
下列範例會產生 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;
}
}