Skip to main content

How to Resolve Error "CS0201: Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement" in C#

The Compiler Error CS0201 is a Syntax/Statement error. The message reads: "Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement".

In C#, not every line of code you write counts as a valid "action." A Statement must actually do something: change a variable, call a method, or create an object. This error occurs when you write an expression that evaluates to a value (like x + y or x == 5) but you don't do anything with that result. The code sits there, calculates a value, and then immediately discards it, which the compiler flags as invalid.

This guide explains how to identify these "useless" lines of code and fix them.

Understanding Valid Statements

To the C# compiler, a standalone line of code (terminated by a semicolon ;) must perform a side effect.

Valid Statements:

  • Assignment: x = 5;
  • Call: Console.WriteLine("Hi");
  • Increment: i++;
  • New Object: new List<int>(); (Though usually pointless without assignment, it is valid).
  • Await: await Task.Delay(1);

Invalid Statements (Expressions):

  • x == 5; (Checks equality, but doesn't use the True/False result).
  • x + y; (Adds numbers, but doesn't store the sum).
  • "Hello"; (A string literal sitting alone).

Scenario 1: Comparisons as Statements (== vs =)

This is the most common typo. You intend to assign a value to a variable, but you accidentally type the double-equals operator == (equality check) instead of the single-equals operator = (assignment).

Example of error

public void UpdateScore()
{
int score = 0;

// ⛔️ Error CS0201: Only assignment, call... can be used as a statement.
// This line asks: "Does score equal 10?" -> Returns False.
// The result 'False' is immediately thrown away.
score == 10;
}

Solution: Use Assignment

Change the operator to =.

public void UpdateScore()
{
int score = 0;

// ✅ Correct: Assigns 10 to score.
score = 10;
}

Scenario 2: Mathematical Expressions without Assignment

Sometimes developers write a math operation expecting it to update the variable in place, similar to how x++ works. However, operations like x + 1 simply return a new value; they do not modify x.

Example of error

public void Increment()
{
int x = 5;

// ⛔️ Error CS0201: "Only assignment... can be used as a statement"
// This calculates '6' but doesn't put it anywhere.
x + 1;
}

Solution: Assign the Result

You must assign the result back to the variable or a new variable.

public void Increment()
{
int x = 5;

// ✅ Correct: Updates x
x = x + 1;

// Or use compound assignment:
x += 1;
}

Scenario 3: Unassigned Method Return Values

This scenario often happens during refactoring. You call a method that returns a value (like a string transformation), but you forget to save that value. While calling a method is usually valid, sometimes the syntax makes it look like a property access or a detached expression.

note

Calling a method GetInfo(); is valid even if you ignore the return value. However, property access obj.Name; is CS0201.

Mistake (Property Access)

public class User
{
public string Name { get; set; }
}

public void CheckUser()
{
var u = new User();

// ⛔️ Error CS0201: Just retrieving the property does nothing.
u.Name;
}

Solution: Use the Value

Pass it to a method, assign it, or remove the line if it is unnecessary.

public void CheckUser()
{
var u = new User();

// ✅ Correct: Printing the value
Console.WriteLine(u.Name);

// ✅ Correct: Assigning the value
string n = u.Name;
}

Mistake (String Replace)

Strings are immutable. Calling .Replace returns a new string; it does not change the original.

string s = "Hello";
// Valid syntax (method call), but logic bug (value discarded).
// However, if you wrote it as an expression body incorrectly:
Action a = () => s.Replace("H", "J"); // This is valid lambda syntax.

// But this standalone line inside a method:
// s.Replace("H", "J"); // Valid CS (Method call), but useless.

// This triggers CS0201:
s.Length; // Accessing property without doing anything.

Conclusion

CS0201 is the compiler asking: "What did you intend to do with this result?"

  1. Check Operators: Did you type == (Comparison) instead of = (Assignment)?
  2. Check Math: Did you type x + 1 instead of x += 1?
  3. Check Access: Are you accessing a property (like list.Count;) without assigning it to a variable or passing it to a method?