C# Error CS0201 – Only assignment, call, increment, decrement, and new object expressions can be used as a statement

C# Compiler Error

CS0201 – Only assignment, call, increment, decrement, and new object expressions can be used as a statement

Reason for the Error

You will receive this error when you have an invalid statement in your C# code. For example, you have an statement that end with a semicolon and doesnot have =, () , new — or ++ operation in it.

For example, try to compile the below code snippet.

namespace DeveloperPubNamespace
{
    class Program
    {
        static void Main(string[] args)
        {
            8 * 2;
        }
    }

}

This will result with the error code CS0201 as you have used 8 * 2 in a statement but doesnot contain any assignment operation.

Error CS0201 Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement ConsoleApp3 C:\Users\SenthilBalu\source\repos\ConsoleApp3\ConsoleApp3\Program.cs 7 Active

C# Error CS0201 - Only assignment, call, increment, decrement, and new object expressions can be used as a statement

Solution

To fix the error code CS0201, you will need to ensure that the statement is valid. You can fix the above code by using the assignment operation.

namespace DeveloperPubNamespace
{
    class Program
    {
        static void Main(string[] args)
        {
            int i = 8 * 2;
        }
    }

}

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