Error del compilador CS0559

Actualización: noviembre 2007

Mensaje de error

El tipo de parámetro para el operador ++ o -- debe ser el tipo contenedor
The parameter type for ++ or -- operator must be the containing type

La declaración de método para la sobrecarga de un operador debe seguir ciertas normas. Para los operadores ++ y --, se requiere que el parámetro sea del mismo tipo que provoca la sobrecarga del operador.

Ejemplo

El código siguiente genera el error CS0559:

// CS0559.cs
// compile with: /target:library
public class iii
{
   public static implicit operator int(iii x)
   {
      return 0;
   }

   public static implicit operator iii(int x)
   {
      return null;
   }

   public static int operator ++(int aa)   // CS0559
   // try the following line instead
   // public static iii operator ++(iii aa)
   {
      return (iii)0;
   }
}

El código siguiente genera el error CS0559.

// CS0559_b.cs
// compile with: /target:library
public struct S
{
   public static S operator ++(S? s) { return new S(); }   // CS0559
   public static S operator --(S? s) { return new S(); }   // CS0559
}

public struct T
{
// OK
   public static T operator --(T t) { return new T(); }
   public static T operator ++(T t) { return new T(); }

   public static T? operator --(T? t) { return new T(); }
   public static T? operator ++(T? t) { return new T(); }
}