Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
The compiler generates the following errors for incorrect use of preprocessor directives:
- CS1024: Preprocessor directive expected
- CS1025: Single-line comment or end-of-line expected
- CS1027: #endif directive expected
- CS1028: Unexpected preprocessor directive
- CS1029: #error: 'text'
- CS1030: #warning: 'text'
- CS1032: Cannot define/undefine preprocessor symbols after first token in file
- CS1038: #endregion directive expected
- CS1040: Preprocessor directives must appear as the first non-white-space character on a line
- CS1517: Invalid preprocessor expression
- CS1560: Invalid filename specified for preprocessor directive. Filename is too long or not a valid filename
- CS1576: The line number specified for #line directive is missing or invalid
- CS1578: Filename, single-line comment or end-of-line expected
- CS1633: Unrecognized #pragma directive
- CS1634: Expected disable or restore
- CS1635: Cannot restore warning 'warning code' because it was disabled globally
- CS1691: 'number' is not a valid warning number
- CS1692: Invalid number
- CS1694: Invalid filename specified for preprocessor directive. Filename is too long or not a valid filename
- CS1695: Invalid #pragma checksum syntax; should be #pragma checksum "filename" "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}" "XXXX..."
- CS1696: Single-line comment or end-of-line expected
- CS1709: Filename specified for preprocessor directive is empty
- CS7009: Cannot use #r after first token in file
- CS7010: Quoted file name expected
- CS7011: #r is only allowed in scripts
- CS8097: #load is only allowed in scripts
- CS8098: Cannot use #load after first token in file
- CS8938: The #line directive value is missing or out of range
- CS8939: The #line directive end position must be greater than or equal to the start position
- CS8996: Raw string literals are not allowed in preprocessor directives
- CS9028: The #line span directive requires space before the first parenthesis, before the character offset, and before the file name
- CS9297:
#:directives cannot be after first token in file - CS9298:
#:directives can be only used in file-based programs (-features:FileBasedProgram) - CS9299:
#:directives cannot be after#ifdirective - CS9314:
#!directives can be only used in scripts or file-based programs
Invalid preprocessor directive syntax
- CS1024: Preprocessor directive expected
- CS1025: Single-line comment or end-of-line expected
- CS1027: #endif directive expected
- CS1028: Unexpected preprocessor directive
- CS1038: #endregion directive expected
- CS1040: Preprocessor directives must appear as the first non-white-space character on a line
- CS1517: Invalid preprocessor expression
- CS1633: Unrecognized #pragma directive
- CS1696: Single-line comment or end-of-line expected
- CS8996: Raw string literals are not allowed in preprocessor directives
These errors indicate that you used invalid syntax for preprocessor directives. Common causes include:
- Using an unrecognized directive after
#(CS1024, CS1633). - Including multiline comments on directive lines (CS1025, CS1696).
- Using directives in unexpected locations (CS1028).
- Missing required matching directives (CS1027, CS1038).
- Not defining or undefining symbols as the first token in the file (CS1032)
- Not placing the directive as the first token on a line (CS1040).
- Using invalid expressions in conditional compilation (CS1517).
- Using raw string literals in preprocessor directives (CS8996).
CS1024 example - Preprocessor directive expected:
#import System // CS1024 - "import" is not a valid directive
CS1025 example - Single-line comment or end-of-line expected:
#if true /* hello
*/ // CS1025 - multiline comment not allowed
#endif
CS1027 example - #endif directive expected:
#if true // CS1027 - missing #endif
class Test { }
CS1028 example - Unexpected preprocessor directive:
#endif // CS1028 - no matching #if
CS1032 example - #define and #undef preprocessor directives must appear before other tokens:
/* Comment */
#define X // CS1032 - directive not first token in file
CS1038 example - #endregion directive expected:
#region testing
class Test { }
// CS1038 - missing #endregion
CS1040 example - Preprocessor directives must appear as the first non-white-space character:
/* Comment */ #define X // CS1040 - directive not first on line
CS1517 example - Invalid preprocessor expression:
#if 1 // CS1517 - numeric literals not allowed
#endif
#if ~symbol // CS1517 - bitwise operators not allowed
#endif
CS1633 example - Unrecognized #pragma directive:
#pragma unknown // CS1633 - "unknown" is not a valid pragma
CS8996 example - Raw string literals are not allowed in preprocessor directives:
// CS8996.cs
#pragma checksum """raw_string""" "{406EA660-64CF-4C82-B6F0-42D48172A799}" "hash" // CS8996
class Test { }
To fix this error, use a regular string literal:
#pragma checksum "filename.cs" "{406EA660-64CF-4C82-B6F0-42D48172A799}" "hash" // OK
class Test { }
To fix these errors, ensure your preprocessor directives follow the correct syntax rules described in the preprocessor directives documentation.
#error and #warning directive errors
- CS1029: #error: 'text'
- CS1030: #warning: 'text'
- CS1634: Expected disable or restore
- CS1635: Cannot restore warning 'warning code' because it was disabled globally
- CS1691: 'number' is not a valid warning number
- CS1692: Invalid number
These errors occur when the compiler processes #error, #warning, and #pragma warning directives. These directives allow you to generate custom error and warning messages during compilation and control warning behavior.
CS1029 displays the text of an error defined with the #error directive:
// CS1029.cs
class Sample
{
static void Main()
{
#error Let's give an error here // CS1029
}
}
Compilation produces the following output:
example.cs(9,8): error CS1029: #error: 'Let's give an error here // CS1029 '
CS1030 displays the text of a warning defined with the #warning directive:
// CS1030.cs
class Sample
{
static void Main()
{
#warning Let's give a warning here
}
}
Compilation produces the following output:
example.cs(6,16): warning CS1030: #warning: 'Let's give a warning here'
CS1634 occurs when a #pragma warning clause is badly formed, such as when disable or restore is omitted:
// CS1634.cs
// compile with: /W:1
#pragma warning // CS1634
// Try this instead:
// #pragma warning disable 0219
class MyClass
{
public static void Main()
{
}
}
CS1635 occurs when you use the /nowarn command line option or project setting to disable a warning globally, but use #pragma warning restore to attempt to restore that warning:
// CS1635.cs
// compile with: /w:1 /nowarn:162
enum MyEnum {one=1,two=2,three=3};
class MyClass
{
public static void Main()
{
#pragma warning disable 162
if (MyEnum.three == MyEnum.two)
System.Console.WriteLine("Duplicate");
#pragma warning restore 162 // CS1635
}
}
CS1691 occurs when a number passed to the #pragma warning directive is not a valid warning number:
// CS1691.cs
public class C
{
int i = 1;
public static void Main()
{
C myC = new C();
#pragma warning disable 151 // CS1691
// Try the following line instead:
// #pragma warning disable 1645
myC.i++;
#pragma warning restore 151 // CS1691
// Try the following line instead:
// #pragma warning restore 1645
}
}
CS1692 occurs when a number in preprocessor directives like #pragma and #line is invalid because it's too large, in the wrong format, or contains illegal characters:
// CS1692.cs
#pragma warning disable a // CS1692
// Try this instead:
// #pragma warning disable 1691
class A
{
static void Main()
{
}
}
These directives are useful for conditional compilation scenarios where you want to alert developers about specific conditions in the code, or control which warnings are displayed during compilation. For more information about using these directives, see the preprocessor directives documentation and #pragma warning.
#line and file directive errors
- CS1560: Invalid filename specified for preprocessor directive. Filename is too long or not a valid filename
- CS1576: The line number specified for #line directive is missing or invalid
- CS1578: Filename, single-line comment or end-of-line expected
- CS1694: Invalid filename specified for preprocessor directive. Filename is too long or not a valid filename
- CS1695: Invalid #pragma checksum syntax; should be #pragma checksum "filename" "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}" "XXXX..."
- CS1709: Filename specified for preprocessor directive is empty
- CS8938: The #line directive value is missing or out of range
- CS8939: The #line directive end position must be greater than or equal to the start position
- CS9028: The #line span directive requires space before the first parenthesis, before the character offset, and before the file name
These errors indicate incorrect usage of the #line directive or file-related preprocessor directives. Common causes include:
- Invalid or missing filenames (CS1560, CS1694, CS1709).
- Incorrect line number format or values (CS1576, CS8938, CS8939).
- Missing proper syntax for filenames and comments (CS1578).
- Malformed
#pragma checksumsyntax (CS1695). - Improper spacing in
#linespan directives (CS9028).
CS1560/CS1694 example - Invalid filename specified:
#line 100 "MyFile1234567890MyFile1234567890MyFile1234567890MyFile1234567890MyFile1234567890MyFile1234567890MyFile1234567890MyFile1234567890MyFile1234567890MyFile1234567890MyFile1234567890MyFile1234567890MyFile1234567890MyFile1234567890MyFile1234567890MyFile1234567890.txt" // CS1560/CS1694 - filename too long
CS1576 example - Line number missing or invalid:
#line "abc.sc" // CS1576 - missing line number
#line abc "file.cs" // CS1576 - invalid line number format
CS1578 example - Filename, comment or end-of-line expected:
#line 101 abc.cs // CS1578 - filename not quoted
CS1695 example - Invalid #pragma checksum syntax:
#pragma checksum "12345" // CS1695 - missing GUID and checksum
CS1709 example - Empty filename:
#pragma checksum "" "{406EA660-64CF-4C82-B6F0-42D48172A799}" "" // CS1709 - empty filename
CS8938 example - #line directive value missing or out of range:
#line // CS8938 - missing value
#line 0 // CS8938 - out of range (must be 1-16,707,566)
CS8939 example - #line end position error:
#line (1, 10) - (1, 5) "file.cs" // CS8939 - end column < start column
CS9028 example - #line span directive spacing:
#line(1, 1) - (1, 10)"file.cs" // CS9028 - missing spaces
To fix these errors, ensure your #line directives and file-related preprocessor directives follow the correct syntax as described in the preprocessor directives documentation.
Incorrect use of file-based apps directives
- CS9297:
#:directives cannot be after first token in file - CS9298:
#:directives can be only used in file-based programs (-features:FileBasedProgram) - CS9299:
#:directives cannot be after#ifdirective - CS9314:
#!directives can be only used in scripts or file-based programs
These errors indicate that you used the #: directives for a file-based app incorrectly. You can learn more about the syntax for these directives in the article on preprocessor directives in the section on file-based apps. Or, you can explore file based apps by following the tutorial on file-based apps.
File contains script.cs directives
The following errors indicate script.cs syntax in a compiled C# file:
- CS7009: Cannot use #r after first token in file
- CS7010: Quoted file name expected
- CS7011: #r is only allowed in scripts
- CS8097: #load is only allowed in scripts
These directives aren't supported in compiled C#. You must remove them, or use script.cs.