Compiler Warning (level 1) CS0420

“identifier”:对 volatile 字段的引用不被视为 volatile

volatile 字段不能像正常情况那样使用 refout 参数进行传递,因为它在函数的范围内不会被视为 volatile。这也有例外情况,例如当调用互锁 API 时。对于任何警告,您可以使用 #pragma warning 在您特意使用 volatile 字段作为引用参数的情况(这种情况很少出现)中禁用此警告。

下面的示例生成 CS0420:

// CS0420.cs
// compile with: /W:1
using System;

class TestClass
{
   private volatile int i;

   public void TestVolatile(ref int ii)
   {
   }

   public static void Main()
   {
      TestClass x = new TestClass();
      x.TestVolatile(ref x.i);   // CS0420 
   }
}