Läs på engelska

Dela via


Kompilatorfel CS0172

Det går inte att fastställa typen av villkorsuttryck eftersom "type1" och "type2" implicit konverteras till varandra

I en villkorsstyrd instruktion måste du implicit kunna konvertera typerna på båda sidor av : token. Det kan inte heller finnas ömsesidiga implicita konverteringar. du behöver bara en konvertering.

Följande exempel genererar 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;  
   }  
}  

Se även