C# Error CS1059 – The operand of an increment or decrement operator must be a variable, property or indexer

C# Error

CS1059 – The operand of an increment or decrement operator must be a variable, property or indexer

Reason for the Error & Solution

The operand of an increment or decrement operator must be a variable, property or indexer.

This error is raised when you try to increment or decrement a constant value. It can also occur if you try to increment an expression such as (a+b)++.

To correct this error

  • Make the variable non-const.

  • Remove the increment or decrement operator.

  • Store the expression in a variable, and then increment the variable.

Example

The following example generates CS1059 because i is a constant, not a variable, and E is an Enum type, whose elements are also constant values.

// CS1059.cs  
    class Program  
    {  
        public enum E : sbyte  
        {  
            a = 1,  
            b = 2  
        }  
  
        static void Main(string[] args)  
        {  
            const int i = 0;  
            i++;            // CS1059  
            E test = E.a++; // CS1059  
        }  
    }  

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...