C# Error CS1019 – Overloadable unary operator expected

C# Error

CS1019 – Overloadable unary operator expected

Reason for the Error & Solution

Overloadable unary operator expected

Something that looks like an overloaded unary operator has been declared, but the operator is missing or is in the wrong location in the signature.

A unary operator is an operator that operates on a single operand. For example, ++ is a unary operator. You can overload some unary operators by using the operator keyword and specifying a single parameter of the type that the operator operates on. For example, if you want to overload the operator ++ for a user-defined class Temperature so that you can write Temperature++, you can define it in this way:

public static  Temperature operator ++ (Temperature temp)  
{  
    temp.Degrees++;  
    return temp;  
}  

When you receive this error, you have declared something that looks like an overloaded unary operator, except that the operator itself is missing or is in the wrong location in the signature. If you remove the ++ from the signature in the previous example, you will generate CS1019.

The following code generates CS1019:

// CS1019.cs  
public class ii  
{  
   int i  
   {  
      get  
      {  
         return 0;  
      }  
   }  
}  
  
public class a  
{  
    public int i;  
// Generates CS1019: "ii" is not a unary operator.  
   public static a operator ii(a aa)
  
   // Use the following line instead:  
   //public static a operator ++(a aa)  
   {  
      aa.i++;  
      return aa;
   }  
  
   public static void Main()  
   {  
   }  
}  

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

C# Compiler Error CS0442 – ‘Property’: abstract properties cannot have private accessors Reason for the Error You’ll get this error...
This is a really simple one . Below is a simple example of an enum called “Designation” defined with the...
This blog post explain the usage of the Checked Block in .NET and how you can use them in Visual...