Compiler Error CS0201

只有 assignment、call、increment、decrement 和 new 对象表达式可用作语句

编译器在遇到无效语句时会生成错误。无效语句是以未表示以下各项的分号结尾的任意行或一系列行:赋值 (=)、方法调用 ()new--++ 运算。有关更多信息,请参见语句、表达式和运算符(C# 编程指南)

下面的示例将生成 CS0201,因为 2 * 3 是表达式,而不是语句。若要使代码能够编译,请尝试将表达式的值赋给变量。

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

下面的示例将生成 CS0201,因为 checked 本身并不是语句,即使已通过递增操作对其进行了参数化。

// 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++; 
         }
      }
   }
}

请参阅

C# Compiler Errors