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

This C# program calculates and displays an upper triangular matrix based on user input. Problem Statement: The program takes the...
This C# program serves as a demonstration of bitwise operators, which are fundamental operators used for manipulating individual bits in...
This C# program is designed to interchange or swap the columns of a matrix. A matrix is a two-dimensional array...