Expand Minimize
0 out of 1 rated this helpful - Rate this topic

Compiler Error CS0201

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

The compiler generates an error when it encounters an invalid statement. An invalid statement is any line or series of lines ending in a semicolon that does not represent an assignment (=), method call (), new, -- or ++ operation. For more information, see Statements, Expressions, and Operators (C# Programming Guide).

The following sample generates CS0201 because 2 * 3 is an expression, not a statement. To make the code compile, try assigning the value of the expression to a variable.

// CS0201.cs
public class MainClass
{
   public static void Main()
   {
      2 * 3;   // CS0201
      // Try the following line instead.
      //   int i = 2 * 3;
   }
}

The following sample generates CS0201 because checked by itself is not a statement, even though it is parameterized by an increment operation.

// CS0201_b.cs
// compile with: /target:library
public class MyList<T> 
{
   public void Add(T x)
   {
      int i = 0;
      if ( (object)x == null)
      {
         checked(i++);   // CS0201

         // OK
         checked {
            i++; 
         }
      }
   }
}
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.