Compiler Warning (level 1) CS0420
'identifier': a reference to a volatile field will not be treated as volatile
A volatile field should not normally be passed using a ref
or out parameter, since it will not be treated as volatile within the scope of the function. There are exceptions to this, such as when calling an interlocked API. As with any warning, you may use the #pragma warning to disable this warning in those rare cases where you are intentionally using a volatile field as a reference parameter.
The following sample generates 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
}
}
Collaborate with us on GitHub
The source for this content can be found on GitHub, where you can also create and review issues and pull requests. For more information, see our contributor guide.
Feedback
https://aka.ms/ContentUserFeedback.
Coming soon: Throughout 2024 we will be phasing out GitHub Issues as the feedback mechanism for content and replacing it with a new feedback system. For more information see:Submit and view feedback for