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

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