编译器警告(等级 1)CS0420

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

volatile 字段通常不应使用 ref 或 out 参数传递,因为在函数作用域内,它不会视为 volatile。 也有例外,如调用互锁 API 时。 对于任何警告,在有意将 volatile 字段用作引用参数的少数情况下,可以使用 #pragma warning 禁用此警告。

下面的示例生成 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
   }  
}