You receive the "CS1513" and "CS1022" error messages when you use the volatile modifier on local variables in Visual C# .NET or in Visual C# 2005 (814907)



The information in this article applies to:

  • Microsoft Visual C# 2005, Express Edition
  • Microsoft Visual C# .NET (2003)

SYMPTOMS

When you use the volatile modifier on local variables in Visual C# .NET or Visual C# 2005 code, you receive the following compiler error messages:

error CS1513: } expected
error CS1022: Type or namespace definition, or end-of-file expected
Note In Visual C# 2005, you receive the following compiler error message:
error CS0246: The type or namespace name 'STAThread' could not be found (are you missing a using directive or an assembly reference?

CAUSE

You cannot use the volatile modifier on local variables or on function variables. Use the volatile modifier only with class variables.

STATUS

This behavior is by design.

WORKAROUND

To work around this behavior, declare the volatile variables at class scope as follows:
class test
{
    volatile int x = 0;

    [System.STAThread]
    static void Main()
    { 
    }
}

MORE INFORMATION

Steps to Reproduce the Behavior

  1. Click Start, and then point to All Programs.
  2. Point to Microsoft Visual Studio .NET, point to Visual Studio .NET Tools, and then click Visual Studio .NET Command Prompt. This starts a console with the environment set to use the Visual Studio .NET command-line build tools.

    Note In Visual Studio 2005, point to Microsoft Visual Studio 2005, point to Visual Studio 2005 Tools, and then click Visual Studio 2005 Command Prompt.
  3. Start Notepad, and then paste the following code:
    class Test
     {
           [STAThread]
           static void Main()
           { 
                   volatile int x = 0;
           }
     }
  4. Save the file as volatile.cs
  5. Use the following command from the Visual Studio .NET 2003 command prompt or the Visual Studio 2005 command prompt to compile the code:
    csc volatile.cs
You receive the compiler error messages that are described in the "Symptoms" section of this article.

Modification Type:MajorLast Reviewed:1/19/2006
Keywords:kbCompiler kbprb KB814907 kbAudDeveloper kbAudITPRO