From ab406937ac5fb18938de7e9e56aac367cb06b171 Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Tue, 7 Oct 2025 13:16:56 -0400 Subject: [PATCH 1/8] Create skeleton --- .../compiler-messages/preprocessor-errors.md | 23 +++++++++++++++++++ docs/csharp/language-reference/toc.yml | 4 ++++ ...n-t-have-specifics-on-this-csharp-error.md | 20 +++++++++++----- 3 files changed, 41 insertions(+), 6 deletions(-) create mode 100644 docs/csharp/language-reference/compiler-messages/preprocessor-errors.md diff --git a/docs/csharp/language-reference/compiler-messages/preprocessor-errors.md b/docs/csharp/language-reference/compiler-messages/preprocessor-errors.md new file mode 100644 index 0000000000000..bb751d396a363 --- /dev/null +++ b/docs/csharp/language-reference/compiler-messages/preprocessor-errors.md @@ -0,0 +1,23 @@ +--- +title: Errors and warnings generated by the C# preprocessor +description: Learn how to diagnose and fix errors related to preprocessor tokens how they are used in C# source files. +f1_keywords: + - "CS9297" + - "CS9298" + - "CS9299" + - "CS9314" +helpviewer_keywords: + - "CS9297" + - "CS9298" + - "CS9299" + - "CS9314" +ms.date: 10/07/2025 +--- +# Preprocessor errors and warnings + +The compiler generates the following errors for incorrect use of preprocessor 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 `#if` directive* +- **CS9314**: *`#!` directives can be only used in scripts or file-based programs* diff --git a/docs/csharp/language-reference/toc.yml b/docs/csharp/language-reference/toc.yml index 4478184281dab..0885c2bbfa453 100644 --- a/docs/csharp/language-reference/toc.yml +++ b/docs/csharp/language-reference/toc.yml @@ -460,6 +460,10 @@ items: - name: C# compiler messages href: ./compiler-messages/index.md items: + - name: Preprocessor errors + href: ./compiler-messages/preprocessor-errors.md + displayName: > + CS9297, CS9298, CS9299, CS9314 - name: Feature or version missing href: ./compiler-messages/feature-version-errors.md displayName: > diff --git a/docs/csharp/misc/sorry-we-don-t-have-specifics-on-this-csharp-error.md b/docs/csharp/misc/sorry-we-don-t-have-specifics-on-this-csharp-error.md index ceefdca01b4b5..e47cead8bcc3a 100644 --- a/docs/csharp/misc/sorry-we-don-t-have-specifics-on-this-csharp-error.md +++ b/docs/csharp/misc/sorry-we-don-t-have-specifics-on-this-csharp-error.md @@ -620,10 +620,6 @@ f1_keywords: - "CS9235" - "CS9236" # C# 14 errors begin here -## Shebang - - "CS9297" - - "CS9298" - - "CS9299" ## using `extension` as type name. (valid identifiers). - "CS9306" ## User defined operators @@ -632,10 +628,22 @@ f1_keywords: - "CS9311" - "CS9312" - "CS9313" -## Shebang - - "CS9314" ## Strings in the data section: - "CS9315" +## More extension errors + - "CS9316" + - "CS9317" + - "CS9318" + - "CS9319" + - "CS9326" + - "CS9327" + - "CS9328" + - "CS9329" + - "CS9330" + - "CS9331" + - "CS9332" + - "CS9333" + - "CS9334" helpviewer_keywords: - "errors [C#], additional information" --- From 5139b2705a056ef430db9ac14064015b768fb862 Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Tue, 7 Oct 2025 14:29:52 -0400 Subject: [PATCH 2/8] Consolidate all existing errors Consolidate existing errors and warnings related to preprocessor directives into this one file. --- .github/prompts/error-consolidation.md | 1 + .openpublishing.redirection.csharp.json | 76 +++ .../compiler-messages/cs1029.md | 34 -- .../compiler-messages/cs1691.md | 38 -- .../compiler-messages/preprocessor-errors.md | 464 ++++++++++++++++++ docs/csharp/language-reference/toc.yml | 3 +- docs/csharp/misc/cs1027.md | 33 -- docs/csharp/misc/cs1028.md | 33 -- docs/csharp/misc/cs1032.md | 31 -- docs/csharp/misc/cs1038.md | 32 -- docs/csharp/misc/cs1040.md | 33 -- docs/csharp/misc/cs1517.md | 46 -- docs/csharp/misc/cs1560.md | 32 -- docs/csharp/misc/cs1576.md | 31 -- docs/csharp/misc/cs1578.md | 31 -- docs/csharp/misc/cs1633.md | 30 -- docs/csharp/misc/cs1634.md | 35 -- docs/csharp/misc/cs1635.md | 37 -- docs/csharp/misc/cs1692.md | 35 -- docs/csharp/misc/cs1694.md | 25 - docs/csharp/misc/cs1695.md | 34 -- docs/csharp/misc/cs1696.md | 31 -- docs/csharp/misc/cs1709.md | 30 -- 23 files changed, 543 insertions(+), 632 deletions(-) create mode 100644 .github/prompts/error-consolidation.md delete mode 100644 docs/csharp/language-reference/compiler-messages/cs1029.md delete mode 100644 docs/csharp/language-reference/compiler-messages/cs1691.md delete mode 100644 docs/csharp/misc/cs1027.md delete mode 100644 docs/csharp/misc/cs1028.md delete mode 100644 docs/csharp/misc/cs1032.md delete mode 100644 docs/csharp/misc/cs1038.md delete mode 100644 docs/csharp/misc/cs1040.md delete mode 100644 docs/csharp/misc/cs1517.md delete mode 100644 docs/csharp/misc/cs1560.md delete mode 100644 docs/csharp/misc/cs1576.md delete mode 100644 docs/csharp/misc/cs1578.md delete mode 100644 docs/csharp/misc/cs1633.md delete mode 100644 docs/csharp/misc/cs1634.md delete mode 100644 docs/csharp/misc/cs1635.md delete mode 100644 docs/csharp/misc/cs1692.md delete mode 100644 docs/csharp/misc/cs1694.md delete mode 100644 docs/csharp/misc/cs1695.md delete mode 100644 docs/csharp/misc/cs1696.md delete mode 100644 docs/csharp/misc/cs1709.md diff --git a/.github/prompts/error-consolidation.md b/.github/prompts/error-consolidation.md new file mode 100644 index 0000000000000..6ef92ab117db0 --- /dev/null +++ b/.github/prompts/error-consolidation.md @@ -0,0 +1 @@ +Add the contents of cs1027.md to the preprocessor-errors.md file, sort that file by 'source_path_from_root'. Add a redirection for cs1027.md to point to preprocessor-errors.md, add "cs1027: to the list of display names in the TOC for preprocessor-errors.md. Finally, delete cs1027.md diff --git a/.openpublishing.redirection.csharp.json b/.openpublishing.redirection.csharp.json index b98367dfd9bed..b8af37ed1fbd0 100644 --- a/.openpublishing.redirection.csharp.json +++ b/.openpublishing.redirection.csharp.json @@ -363,6 +363,10 @@ "source_path_from_root": "/docs/csharp/language-reference/compiler-messages/cs1683.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/assembly-references" }, + { + "source_path_from_root": "/docs/csharp/language-reference/compiler-messages/cs1691.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/preprocessor-errors" + }, { "source_path_from_root": "/docs/csharp/language-reference/compiler-messages/cs0270.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/array-declaration-errors" @@ -1834,6 +1838,58 @@ "source_path_from_root": "/docs/csharp/misc/cs1020.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/overload-resolution" }, + { + "source_path_from_root": "/docs/csharp/misc/cs1027.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/preprocessor-errors" + }, + { + "source_path_from_root": "/docs/csharp/misc/cs1028.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/preprocessor-errors" + }, + { + "source_path_from_root": "/docs/csharp/language-reference/compiler-messages/cs1029.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/preprocessor-errors" + }, + { + "source_path_from_root": "/docs/csharp/misc/cs1032.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/preprocessor-errors" + }, + { + "source_path_from_root": "/docs/csharp/misc/cs1038.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/preprocessor-errors" + }, + { + "source_path_from_root": "/docs/csharp/misc/cs1040.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/preprocessor-errors" + }, + { + "source_path_from_root": "/docs/csharp/misc/cs1517.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/preprocessor-errors" + }, + { + "source_path_from_root": "/docs/csharp/misc/cs1560.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/preprocessor-errors" + }, + { + "source_path_from_root": "/docs/csharp/misc/cs1576.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/preprocessor-errors" + }, + { + "source_path_from_root": "/docs/csharp/misc/cs1578.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/preprocessor-errors" + }, + { + "source_path_from_root": "/docs/csharp/misc/cs1633.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/preprocessor-errors" + }, + { + "source_path_from_root": "/docs/csharp/misc/cs1634.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/preprocessor-errors" + }, + { + "source_path_from_root": "/docs/csharp/misc/cs1635.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/preprocessor-errors" + }, { "source_path_from_root": "/docs/csharp/misc/cs1100.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/extension-declarations" @@ -1994,10 +2050,30 @@ "source_path_from_root": "/docs/csharp/misc/cs1686.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/lambda-expression-errors#syntax-limitations-in-lambda-expressions" }, + { + "source_path_from_root": "/docs/csharp/misc/cs1692.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/preprocessor-errors" + }, + { + "source_path_from_root": "/docs/csharp/misc/cs1694.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/preprocessor-errors" + }, + { + "source_path_from_root": "/docs/csharp/misc/cs1695.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/preprocessor-errors" + }, + { + "source_path_from_root": "/docs/csharp/misc/cs1696.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/preprocessor-errors" + }, { "source_path_from_root": "/docs/csharp/misc/cs1706.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/lambda-expression-errors#syntax-limitations-in-lambda-expressions" }, + { + "source_path_from_root": "/docs/csharp/misc/cs1709.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/preprocessor-errors" + }, { "source_path_from_root": "/docs/csharp/misc/cs1714.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/assembly-references" diff --git a/docs/csharp/language-reference/compiler-messages/cs1029.md b/docs/csharp/language-reference/compiler-messages/cs1029.md deleted file mode 100644 index 1c07e0e7c66a6..0000000000000 --- a/docs/csharp/language-reference/compiler-messages/cs1029.md +++ /dev/null @@ -1,34 +0,0 @@ ---- -description: "Compiler Error CS1029" -title: "Compiler Error CS1029" -ms.date: 07/20/2015 -f1_keywords: - - "CS1029" -helpviewer_keywords: - - "CS1029" -ms.assetid: 61bd4d44-9bfc-42bb-a9f0-a0683da53640 ---- -# Compiler Error CS1029 - -\#error: 'text' - -Displays the text of an error defined with the [#error](../preprocessor-directives.md#error-and-warning-information) directive. - -The following sample shows how to create a user-defined error: - -```csharp -// CS1029.cs -class Sample -{ - static void Main() - { - #error Let's give an error here // CS1029 - } -} -``` - -Compilation produces the following output: - -```console -example.cs(9,8): error CS1029: #error: 'Let's give an error here // CS1029 ' -``` diff --git a/docs/csharp/language-reference/compiler-messages/cs1691.md b/docs/csharp/language-reference/compiler-messages/cs1691.md deleted file mode 100644 index 22d63b48b3136..0000000000000 --- a/docs/csharp/language-reference/compiler-messages/cs1691.md +++ /dev/null @@ -1,38 +0,0 @@ ---- -description: "Compiler Warning (level 1) CS1691" -title: "Compiler Warning (level 1) CS1691" -ms.date: 07/20/2015 -f1_keywords: - - "CS1691" -helpviewer_keywords: - - "CS1691" -ms.assetid: 7f0fea4d-4215-446c-a249-57daa7e967d2 ---- -# Compiler Warning (level 1) CS1691 - -'number' is not a valid warning number - - A number that was passed to the [#pragma warning](../preprocessor-directives.md#pragma-warning) preprocessor directive was not a valid warning number. Verify that the number represents a warning, not an error or another sequence of characters. - -## Example - - The following example generates CS1691. - -```csharp -// 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 - } -} -``` diff --git a/docs/csharp/language-reference/compiler-messages/preprocessor-errors.md b/docs/csharp/language-reference/compiler-messages/preprocessor-errors.md index bb751d396a363..547a453b992a9 100644 --- a/docs/csharp/language-reference/compiler-messages/preprocessor-errors.md +++ b/docs/csharp/language-reference/compiler-messages/preprocessor-errors.md @@ -2,11 +2,49 @@ title: Errors and warnings generated by the C# preprocessor description: Learn how to diagnose and fix errors related to preprocessor tokens how they are used in C# source files. f1_keywords: + - "CS1027" + - "CS1028" + - "CS1029" + - "CS1032" + - "CS1038" + - "CS1040" + - "CS1517" + - "CS1560" + - "CS1576" + - "CS1578" + - "CS1633" + - "CS1634" + - "CS1635" + - "CS1691" + - "CS1692" + - "CS1694" + - "CS1695" + - "CS1696" + - "CS1709" - "CS9297" - "CS9298" - "CS9299" - "CS9314" helpviewer_keywords: + - "CS1027" + - "CS1028" + - "CS1029" + - "CS1032" + - "CS1038" + - "CS1040" + - "CS1517" + - "CS1560" + - "CS1576" + - "CS1578" + - "CS1633" + - "CS1634" + - "CS1635" + - "CS1691" + - "CS1692" + - "CS1694" + - "CS1695" + - "CS1696" + - "CS1709" - "CS9297" - "CS9298" - "CS9299" @@ -17,7 +55,433 @@ ms.date: 10/07/2025 The compiler generates the following errors for incorrect use of preprocessor directives: +- **CS1027**: *#endif directive expected* +- **CS1028**: *Unexpected preprocessor directive* +- **CS1029**: *#error: '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* - **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 `#if` directive* - **CS9314**: *`#!` directives can be only used in scripts or file-based programs* + +## CS1027: #endif directive expected + +A matching `#endif` [preprocessor directive](../preprocessor-directives.md#conditional-compilation) was not found for a specified `#if` directive. Or, the compiler may have found a `#endregion` directive when there was no matching `#region` directive inside a `#if` block. + +The following sample generates CS1027: + +```csharp +// CS1027.cs +#if true // CS1027, uncomment next line to resolve +// #endif + +namespace x +{ + public class clx + { + public static void Main() + { + } + } +} +``` + +## CS1028: Unexpected preprocessor directive + +A [preprocessor directive](../preprocessor-directives.md) was found but not expected. + +For example, a `#endif` was found with no preceding `#if`. + +The following sample generates CS1028: + +```csharp +// CS1028.cs +#endif // CS1028, no matching #if +namespace x +{ + public class clx + { + public static void Main() + { + } + } +} +``` + +## CS1029: #error: 'text' + +Displays the text of an error defined with the [#error](../preprocessor-directives.md#error-and-warning-information) directive. + +The following sample shows how to create a user-defined error: + +```csharp +// CS1029.cs +class Sample +{ + static void Main() + { + #error Let's give an error here // CS1029 + } +} +``` + +Compilation produces the following output: + +```console +example.cs(9,8): error CS1029: #error: 'Let's give an error here // CS1029 ' +``` + +## CS1032: Cannot define/undefine preprocessor symbols after first token in file + +The `#define` and `#undef` [preprocessor directives](../preprocessor-directives.md#defining-symbols) must be used at the beginning of a program, before any other keywords, such as those used in the namespace declaration. + +The following sample generates CS1032: + +```csharp +// CS1032.cs +namespace x +{ + public class clx + { + #define a // CS1032, put before namespace + public static void Main() + { + } + } +} +``` + +## CS1038: #endregion directive expected + +A [#region](../preprocessor-directives.md#defining-regions) directive did not have a matching [#endregion](../preprocessor-directives.md#defining-regions) directive. + +The following sample generates CS1038: + +```csharp +// CS1038.cs +#region testing + +public class clx +{ + public static void Main() + { + } +} +// CS1038 +// uncomment the next line to resolve +// #endregion +``` + +## CS1040: Preprocessor directives must appear as the first non-white-space character on a line + +A [preprocessor directive](../preprocessor-directives.md) was found on a line and was not the first token on the line. A directive must be the first token on the line. + +The following sample generates CS1040: + +```csharp +// CS1040.cs +/* Define a symbol, X */ #define X // CS1040 + +// try the following two lines instead +// /* Define a symbol, X */ +// #define X + +public class MyClass +{ + public static void Main() + { + } +} +``` + +## CS1517: Invalid preprocessor expression + +The compiler encountered an invalid preprocessor expression. + +For more information, see [Preprocessor Directives](../preprocessor-directives.md). + +The following sample shows some valid and invalid preprocessor expressions: + +```csharp +// CS1517.cs +#if symbol // OK +#endif +#if !symbol // OK +#endif +#if (symbol) // OK +#endif +#if true // OK +#endif +#if false // OK +#endif +#if 1 // CS1517 +#endif +#if ~symbol // CS1517 +#endif +#if * // CS1517 +#endif + +class x +{ + public static void Main() + { + } +} +``` + +## CS1560: Invalid filename specified for preprocessor directive. Filename is too long or not a valid filename + +The file name that was specified with [#line](../preprocessor-directives.md#error-and-warning-information) exceeded _MAX_PATH (256 characters) or the line on which `#line` was found exceeded 2000 characters. + +## Example + +The following sample generates CS1560. + +```csharp +// cs1560.cs +using System; +class MyClass +{ + public static void Main() + { + Console.WriteLine("Normal line #1."); + #line 21 "MyFile1234567890MyFile1234567890MyFile1234567890MyFile1234567890MyFile1234567890MyFile1234567890MyFile1234567890MyFile1234567890MyFile1234567890MyFile1234567890MyFile1234567890MyFile1234567890MyFile1234567890MyFile1234567890MyFile1234567890MyFile1234567890.txt" // CS1560 + } +} +``` + +## CS1576: The line number specified for #line directive is missing or invalid + +The compiler detected an error with the value passed to the [#line](../preprocessor-directives.md#error-and-warning-information) directive. + +The following sample generates CS1576: + +```csharp +// CS1576.cs +public class MyClass +{ + static void Main() + { + #line "abc.sc" // CS1576 + // try the following line instead + //#line 101 "abc.sc" + intt i; // error will be reported on line 101 + } +} +``` + +## CS1578: Filename, single-line comment or end-of-line expected + +After a [#line](../preprocessor-directives.md#error-and-warning-information) directive, only a file name (in double quotation marks) or a single-line comment is allowed. + +The following sample generates CS1578: + +```csharp +// CS1578.cs +class MyClass +{ + static void Main() + { + #line 101 abc.cs // CS1578 + // try the following line instead + //#line 101 "abc.cs" + intt i; // error will be reported on line 101 + } +} +``` + +## CS1633: Unrecognized #pragma directive + +The pragma used was not one of the known pragmas supported by the C# compiler. To resolve this error, use only pragmas supported. + +The following sample generates CS1633: + +```csharp +// CS1633.cs +// compile with: /W:1 +#pragma unknown // CS1633 + +class C +{ + public static void Main() + { + } +} +``` + +## CS1634: Expected disable or restore + +This error occurs if a [#pragma warning](../preprocessor-directives.md#pragma-warning) clause is badly formed, such as if disable or restore was omitted. + +The following sample generates CS1634: + +```csharp +// CS1634.cs +// compile with: /W:1 + +#pragma warning // CS1634 +// Try this instead: +// #pragma warning disable 0219 + +class MyClass +{ + public static void Main() + { + } +} +``` + +## CS1635: Cannot restore warning 'warning code' because it was disabled globally + +This warning occurs if you use the `/nowarn` command line option or project setting to disable a warning for the entire compilation unit, but you use `#pragma warning restore` to attempt to restore that warning. To resolve this error, remove the `/nowarn` command line option or project setting, or remove the `#pragma warning restore` for any warnings you are disabling via the command line or project settings. For more information, see [#pragma warning](../preprocessor-directives.md#pragma-warning). + +The following sample generates CS1635: + +```csharp +// 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 + } +} +``` + +## CS1691: 'number' is not a valid warning number + +A number that was passed to the [#pragma warning](../preprocessor-directives.md#pragma-warning) preprocessor directive was not a valid warning number. Verify that the number represents a warning, not an error or another sequence of characters. + +The following example generates CS1691: + +```csharp +// 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: Invalid number + +A number of preprocessor directives, such as `#pragma` and `#line`, use numbers as parameters. One of these numbers is invalid because it is too big, in the wrong format, contains illegal characters, and so on. To correct this error, correct the number. + +The following example generates CS1692: + +```csharp +// CS1692.cs + +#pragma warning disable a // CS1692 +// Try this instead: +// #pragma warning disable 1691 + +class A +{ + static void Main() + { + } +} +``` + +## CS1694: Invalid filename specified for preprocessor directive. Filename is too long or not a valid filename + +This warning occurs when using the `#pragma checksum` preprocessor directive. The file name specified is longer than 256 characters. To resolve this warning, use a shorter file name. + +The following sample generates CS1694: + +```csharp +// cs1694.cs +#pragma checksum "MyFile1234567890MyFile1234567890MyFile1234567890MyFile1234567890MyFile1234567890MyFile1234567890MyFile1234567890MyFile1234567890MyFile1234567890MyFile1234567890MyFile1234567890MyFile1234567890MyFile1234567890MyFile1234567890MyFile1234567890MyFile1234567890.txt" {00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F} // CS1694 +class MyClass {} +``` + +## CS1695: Invalid #pragma checksum syntax; should be #pragma checksum "filename" "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}" "XXXX..." + +You should rarely encounter this error since the checksum is generally inserted at run time if you are generating code by means of the Code Dom API. + +However, if you were to type in this `#pragma` statement and mistype either the GUID or checksum, you would get this error. The syntax checking by the compiler does not validate that you typed in a correct GUID, but it does check for the right number of digits and delimiters, and that the digits are hexadecimal. Likewise, it verifies that the checksum contains an even number of digits, and that the digits are hexadecimal. + +The following example generates CS1695: + +```csharp +// CS1695.cs + +#pragma checksum "12345" // CS1695 + +public class Test +{ + static void Main() + { + } +} +``` + +## CS1696: Single-line comment or end-of-line expected + +The compiler requires a preprocessor directive to be followed by an end-of-line terminator or by a single-line comment. The compiler has finished processing a valid preprocessor directive, and has encountered something that violates this syntax constraint. + +The following sample generates CS1696: + +```csharp +// CS1696.cs +class Test +{ + public static void Main() + { + #pragma warning disable 1030;219 // CS1696 + #pragma warning disable 1030 // OK + } +} +``` + +## CS1709: Filename specified for preprocessor directive is empty + +You have specified a preprocessor directive that includes a file name, but that file is empty. To resolve this warning, put the needed content into the file. + +The following example generates CS1709: + +```csharp +// CS1709.cs +class Test +{ + static void Main() + { + #pragma checksum "" "{406EA660-64CF-4C82-B6F0-42D48172A799}" "" // CS1709 + } +} +``` diff --git a/docs/csharp/language-reference/toc.yml b/docs/csharp/language-reference/toc.yml index 0885c2bbfa453..6d690dfe2644b 100644 --- a/docs/csharp/language-reference/toc.yml +++ b/docs/csharp/language-reference/toc.yml @@ -463,7 +463,8 @@ items: - name: Preprocessor errors href: ./compiler-messages/preprocessor-errors.md displayName: > - CS9297, CS9298, CS9299, CS9314 + CS1027, CS1028, CS1029, CS1032, CS1038, CS1040, CS1517, CS1560, CS1576, CS1578, CS1633, CS1634, CS1635, CS1691, CS1692, + CS1694, CS1695, CS1696, CS1709, CS9297, CS9298, CS9299, CS9314 - name: Feature or version missing href: ./compiler-messages/feature-version-errors.md displayName: > diff --git a/docs/csharp/misc/cs1027.md b/docs/csharp/misc/cs1027.md deleted file mode 100644 index 813590500c52b..0000000000000 --- a/docs/csharp/misc/cs1027.md +++ /dev/null @@ -1,33 +0,0 @@ ---- -description: "Compiler Error CS1027" -title: "Compiler Error CS1027" -ms.date: 07/20/2015 -f1_keywords: - - "CS1027" -helpviewer_keywords: - - "CS1027" -ms.assetid: a6657f0f-5664-47a5-95cf-803f5a0e0c90 ---- -# Compiler Error CS1027 - -\#endif directive expected - - A matching `#endif` [preprocessor directive](../language-reference/preprocessor-directives.md#conditional-compilation) was not found for a specified `#if` directive. Or, the compiler may have found a `#endregion` directive when there was no matching `#region` directive inside a `#if` block. - - The following sample generates CS1027: - -```csharp -// CS1027.cs -#if true // CS1027, uncomment next line to resolve -// #endif - -namespace x -{ - public class clx - { - public static void Main() - { - } - } -} -``` diff --git a/docs/csharp/misc/cs1028.md b/docs/csharp/misc/cs1028.md deleted file mode 100644 index 091ca2dc55f10..0000000000000 --- a/docs/csharp/misc/cs1028.md +++ /dev/null @@ -1,33 +0,0 @@ ---- -description: "Compiler Error CS1028" -title: "Compiler Error CS1028" -ms.date: 07/20/2015 -f1_keywords: - - "CS1028" -helpviewer_keywords: - - "CS1028" -ms.assetid: 9df07db3-256f-45e9-8323-26539c55a1d8 ---- -# Compiler Error CS1028 - -Unexpected preprocessor directive - - A [preprocessor directive](../language-reference/preprocessor-directives.md) was found but not expected. - - For example, a `#endif` was found with no preceding `#if`. - - The following sample generates CS1028: - -```csharp -// CS1028.cs -#endif // CS1028, no matching #if -namespace x -{ - public class clx - { - public static void Main() - { - } - } -} -``` diff --git a/docs/csharp/misc/cs1032.md b/docs/csharp/misc/cs1032.md deleted file mode 100644 index 5852c43c50242..0000000000000 --- a/docs/csharp/misc/cs1032.md +++ /dev/null @@ -1,31 +0,0 @@ ---- -description: "Compiler Error CS1032" -title: "Compiler Error CS1032" -ms.date: 07/20/2015 -f1_keywords: - - "CS1032" -helpviewer_keywords: - - "CS1032" -ms.assetid: fe318a6c-4403-4b9b-b3d8-753ec31c00ff ---- -# Compiler Error CS1032 - -Cannot define/undefine preprocessor symbols after first token in file - - The `#define` and `#undef` [preprocessor directives](../language-reference/preprocessor-directives.md#defining-symbols) must be used at the beginning of a program, before any other keywords, such as those used in the namespace declaration. - - The following sample generates CS1032: - -```csharp -// CS1032.cs -namespace x -{ - public class clx - { - #define a // CS1032, put before namespace - public static void Main() - { - } - } -} -``` diff --git a/docs/csharp/misc/cs1038.md b/docs/csharp/misc/cs1038.md deleted file mode 100644 index b7068911b42ce..0000000000000 --- a/docs/csharp/misc/cs1038.md +++ /dev/null @@ -1,32 +0,0 @@ ---- -description: "Compiler Error CS1038" -title: "Compiler Error CS1038" -ms.date: 07/20/2015 -f1_keywords: - - "CS1038" -helpviewer_keywords: - - "CS1038" -ms.assetid: 05c53ae9-2819-4771-aee8-3f2ff6bcf0b0 ---- -# Compiler Error CS1038 - -\#endregion directive expected - - A [#region](../language-reference/preprocessor-directives.md#defining-regions) directive did not have a matching [#endregion](../language-reference/preprocessor-directives.md#defining-regions) directive. - - The following sample generates CS1038: - -```csharp -// CS1038.cs -#region testing - -public class clx -{ - public static void Main() - { - } -} -// CS1038 -// uncomment the next line to resolve -// #endregion -``` diff --git a/docs/csharp/misc/cs1040.md b/docs/csharp/misc/cs1040.md deleted file mode 100644 index 9090b5c1c9506..0000000000000 --- a/docs/csharp/misc/cs1040.md +++ /dev/null @@ -1,33 +0,0 @@ ---- -description: "Compiler Error CS1040" -title: "Compiler Error CS1040" -ms.date: 07/20/2015 -f1_keywords: - - "CS1040" -helpviewer_keywords: - - "CS1040" -ms.assetid: a988d665-ead5-489f-922d-ff2c4dd8a922 ---- -# Compiler Error CS1040 - -Preprocessor directives must appear as the first non-white-space character on a line - - A [preprocessor directive](../language-reference/preprocessor-directives.md) was found on a line and was not the first token on the line. A directive must be the first token on the line. - - The following sample generates CS1040: - -```csharp -// CS1040.cs -/* Define a symbol, X */ #define X // CS1040 - -// try the following two lines instead -// /* Define a symbol, X */ -// #define X - -public class MyClass -{ - public static void Main() - { - } -} -``` diff --git a/docs/csharp/misc/cs1517.md b/docs/csharp/misc/cs1517.md deleted file mode 100644 index fcd5549811416..0000000000000 --- a/docs/csharp/misc/cs1517.md +++ /dev/null @@ -1,46 +0,0 @@ ---- -description: "Compiler Error CS1517" -title: "Compiler Error CS1517" -ms.date: 07/20/2015 -f1_keywords: - - "CS1517" -helpviewer_keywords: - - "CS1517" -ms.assetid: 3b0201fb-8fab-4e6a-9ad9-f04c0de89517 ---- -# Compiler Error CS1517 - -Invalid preprocessor expression - - The compiler encountered an invalid preprocessor expression. - - For more information, see [Preprocessor Directives](../language-reference/preprocessor-directives.md). - - The following sample shows some valid and invalid preprocessor expressions: - -```csharp -// CS1517.cs -#if symbol // OK -#endif -#if !symbol // OK -#endif -#if (symbol) // OK -#endif -#if true // OK -#endif -#if false // OK -#endif -#if 1 // CS1517 -#endif -#if ~symbol // CS1517 -#endif -#if * // CS1517 -#endif - -class x -{ - public static void Main() - { - } -} -``` diff --git a/docs/csharp/misc/cs1560.md b/docs/csharp/misc/cs1560.md deleted file mode 100644 index 1840ee0a2a226..0000000000000 --- a/docs/csharp/misc/cs1560.md +++ /dev/null @@ -1,32 +0,0 @@ ---- -description: "Compiler Error CS1560" -title: "Compiler Error CS1560" -ms.date: 07/20/2015 -f1_keywords: - - "CS1560" -helpviewer_keywords: - - "CS1560" -ms.assetid: 772c4543-6c8d-453f-ae3f-d333528eb8b3 ---- -# Compiler Error CS1560 - -Invalid filename specified for preprocessor directive. Filename is too long or not a valid filename - - The file name that was specified with [#line](../language-reference/preprocessor-directives.md#error-and-warning-information) exceeded _MAX_PATH (256 characters) or the line on which `#line` was found exceeded 2000 characters. - -## Example - - The following sample generates CS1560. - -```csharp -// cs1560.cs -using System; -class MyClass -{ - public static void Main() - { - Console.WriteLine("Normal line #1."); - #line 21 "MyFile1234567890MyFile1234567890MyFile1234567890MyFile1234567890MyFile1234567890MyFile1234567890MyFile1234567890MyFile1234567890MyFile1234567890MyFile1234567890MyFile1234567890MyFile1234567890MyFile1234567890MyFile1234567890MyFile1234567890MyFile1234567890.txt" // CS1560 - } -} -``` diff --git a/docs/csharp/misc/cs1576.md b/docs/csharp/misc/cs1576.md deleted file mode 100644 index 40f429e803592..0000000000000 --- a/docs/csharp/misc/cs1576.md +++ /dev/null @@ -1,31 +0,0 @@ ---- -description: "Compiler Error CS1576" -title: "Compiler Error CS1576" -ms.date: 07/20/2015 -f1_keywords: - - "CS1576" -helpviewer_keywords: - - "CS1576" -ms.assetid: 3e39cb80-e7de-4c78-a22a-57e267121a96 ---- -# Compiler Error CS1576 - -The line number specified for #line directive is missing or invalid - - The compiler detected an error with the value passed to the [#line](../language-reference/preprocessor-directives.md#error-and-warning-information) directive. - - The following sample generates CS1576: - -```csharp -// CS1576.cs -public class MyClass -{ - static void Main() - { - #line "abc.sc" // CS1576 - // try the following line instead - //#line 101 "abc.sc" - intt i; // error will be reported on line 101 - } -} -``` diff --git a/docs/csharp/misc/cs1578.md b/docs/csharp/misc/cs1578.md deleted file mode 100644 index 5dd9cfdcf3be1..0000000000000 --- a/docs/csharp/misc/cs1578.md +++ /dev/null @@ -1,31 +0,0 @@ ---- -description: "Compiler Error CS1578" -title: "Compiler Error CS1578" -ms.date: 07/20/2015 -f1_keywords: - - "CS1578" -helpviewer_keywords: - - "CS1578" -ms.assetid: 8356cd33-5acc-4db7-8bbd-2d25f9d7728e ---- -# Compiler Error CS1578 - -Filename, single-line comment or end-of-line expected - - After a [#line](../language-reference/preprocessor-directives.md#error-and-warning-information) directive, only a file name (in double quotation marks) or a single-line comment is allowed. - - The following sample generates CS1578: - -```csharp -// CS1578.cs -class MyClass -{ - static void Main() - { - #line 101 abc.cs // CS1578 - // try the following line instead - //#line 101 "abc.cs" - intt i; // error will be reported on line 101 - } -} -``` diff --git a/docs/csharp/misc/cs1633.md b/docs/csharp/misc/cs1633.md deleted file mode 100644 index c16397abd25fd..0000000000000 --- a/docs/csharp/misc/cs1633.md +++ /dev/null @@ -1,30 +0,0 @@ ---- -description: "Compiler Warning (level 1) CS1633" -title: "Compiler Warning (level 1) CS1633" -ms.date: 07/20/2015 -f1_keywords: - - "CS1633" -helpviewer_keywords: - - "CS1633" -ms.assetid: f31db218-f880-4fc4-ab34-8bcdc49011da ---- -# Compiler Warning (level 1) CS1633 - -Unrecognized #pragma directive - - The pragma used was not one of the known pragmas supported by the C# compiler. To resolve this error, use only pragmas supported. - - The following sample generates CS1633: - -```csharp -// CS1633.cs -// compile with: /W:1 -#pragma unknown // CS1633 - -class C -{ - public static void Main() - { - } -} -``` diff --git a/docs/csharp/misc/cs1634.md b/docs/csharp/misc/cs1634.md deleted file mode 100644 index a55a1ddf22014..0000000000000 --- a/docs/csharp/misc/cs1634.md +++ /dev/null @@ -1,35 +0,0 @@ ---- -description: "Compiler Warning (level 1) CS1634" -title: "Compiler Warning (level 1) CS1634" -ms.date: 07/20/2015 -f1_keywords: - - "CS1634" -helpviewer_keywords: - - "CS1634" -ms.assetid: 4fd00eeb-89e3-4c18-827d-9b00a4bd8c9a ---- -# Compiler Warning (level 1) CS1634 - -Expected disable or restore - - This error occurs if a #pragma warning clause is badly formed, such as if disable or restore was omitted. For more information, see the [#pragma warning](../language-reference/preprocessor-directives.md#pragma-warning) topic. - -## Example - - The following sample generates CS1634: - -```csharp -// CS1634.cs -// compile with: /W:1 - -#pragma warning // CS1634 -// Try this instead: -// #pragma warning disable 0219 - -class MyClass -{ - public static void Main() - { - } -} -``` diff --git a/docs/csharp/misc/cs1635.md b/docs/csharp/misc/cs1635.md deleted file mode 100644 index a277214040856..0000000000000 --- a/docs/csharp/misc/cs1635.md +++ /dev/null @@ -1,37 +0,0 @@ ---- -description: "Compiler Warning (level 1) CS1635" -title: "Compiler Warning (level 1) CS1635" -ms.date: 07/20/2015 -f1_keywords: - - "CS1635" -helpviewer_keywords: - - "CS1635" -ms.assetid: e1795880-f7ea-4dca-b15b-4ba549d7ed78 ---- -# Compiler Warning (level 1) CS1635 - -Cannot restore warning 'warning code' because it was disabled globally - - This warning occurs if you use the **/nowarn** command line option or project setting to disable a warning for the entire compilation unit, but you use `#pragma warning restore` to attempt to restore that warning. To resolve this error, remove the /nowarn command line option or project setting, or remove the `#pragma warning restore` for any warnings you are disabling via the command line or project settings. For more information, see [#pragma warning](../language-reference/preprocessor-directives.md#pragma-warning). - - The following sample generates CS1635: - -```csharp -// 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 - } -} -``` diff --git a/docs/csharp/misc/cs1692.md b/docs/csharp/misc/cs1692.md deleted file mode 100644 index 3ea26f947c4be..0000000000000 --- a/docs/csharp/misc/cs1692.md +++ /dev/null @@ -1,35 +0,0 @@ ---- -description: "Learn more about: Compiler Warning (level 1) CS1692" -title: "Compiler Warning (level 1) CS1692" -ms.date: 07/20/2015 -f1_keywords: - - "CS1692" -helpviewer_keywords: - - "CS1692" -ms.assetid: 1a6d52e1-0ebb-4990-ac0b-36b05a884a19 ---- - -# Compiler Warning (level 1) CS1692 - -Invalid number - -A number of preprocessor directives, such as `#pragma` and `#line`, use numbers as parameters. One of these numbers is invalid because it is too big, in the wrong format, contains illegal characters, and so on. To correct this error, correct the number. - -## Example - -The following example generates CS1692. - -```csharp -// CS1692.cs - -#pragma warning disable a // CS1692 -// Try this instead: -// #pragma warning disable 1691 - -class A -{ - static void Main() - { - } -} -``` diff --git a/docs/csharp/misc/cs1694.md b/docs/csharp/misc/cs1694.md deleted file mode 100644 index d422c19c1f83a..0000000000000 --- a/docs/csharp/misc/cs1694.md +++ /dev/null @@ -1,25 +0,0 @@ ---- -description: "Learn more about: Compiler Warning (level 1) CS1694" -title: "Compiler Warning (level 1) CS1694" -ms.date: 07/20/2015 -f1_keywords: - - "CS1694" -helpviewer_keywords: - - "CS1694" -ms.assetid: 9cb6b5d4-36a0-4b07-9690-14b5105da44b ---- -# Compiler Warning (level 1) CS1694 - -Invalid filename specified for preprocessor directive. Filename is too long or not a valid filename. - - This warning occurs when using the `#pragma checksum` preprocessor directive. The file name specified is longer than 256 characters. To resolve this warning, use a shorter file name. - -## Example - - The following sample generates CS1694. - -```csharp -// cs1694.cs -#pragma checksum "MyFile1234567890MyFile1234567890MyFile1234567890MyFile1234567890MyFile1234567890MyFile1234567890MyFile1234567890MyFile1234567890MyFile1234567890MyFile1234567890MyFile1234567890MyFile1234567890MyFile1234567890MyFile1234567890MyFile1234567890MyFile1234567890.txt" {00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F} // CS1694 -class MyClass {} -``` diff --git a/docs/csharp/misc/cs1695.md b/docs/csharp/misc/cs1695.md deleted file mode 100644 index f091fb5dfc12d..0000000000000 --- a/docs/csharp/misc/cs1695.md +++ /dev/null @@ -1,34 +0,0 @@ ---- -description: "Learn more about: Compiler Warning (level 1) CS1695" -title: "Compiler Warning (level 1) CS1695" -ms.date: 07/20/2015 -f1_keywords: - - "CS1695" -helpviewer_keywords: - - "CS1695" -ms.assetid: cc4e4d00-0618-400d-985b-90968e98772c ---- -# Compiler Warning (level 1) CS1695 - -Invalid #pragma checksum syntax; should be #pragma checksum "filename" "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}" "XXXX..." - - You should rarely encounter this error since the checksum is generally inserted at run time if you are generating code by means of the Code Dom API. - - However, if you were to type in this `#pragma` statement and mistype either the GUID or checksum, you would get this error. The syntax checking by the compiler does not validate that you typed in a correct GUID, but it does check for the right number of digits and delimiters, and that the digits are hexadecimal. Likewise, it verifies that the checksum contains an even number of digits, and that the digits are hexadecimal. - -## Example - - The following example generates CS1695. - -```csharp -// CS1695.cs - -#pragma checksum "12345" // CS1695 - -public class Test -{ - static void Main() - { - } -} -``` diff --git a/docs/csharp/misc/cs1696.md b/docs/csharp/misc/cs1696.md deleted file mode 100644 index a47f40b5ebd26..0000000000000 --- a/docs/csharp/misc/cs1696.md +++ /dev/null @@ -1,31 +0,0 @@ ---- -description: "Learn more about: Compiler Warning (level 1) CS1696" -title: "Compiler Warning (level 1) CS1696" -ms.date: 07/20/2015 -f1_keywords: - - "CS1696" -helpviewer_keywords: - - "CS1696" -ms.assetid: 69a45988-1aba-4a01-a84e-7ca59f8dde28 ---- -# Compiler Warning (level 1) CS1696 - -Single-line comment or end-of-line expected - - The compiler requires a preprocessor directive to be followed by an end-of-line terminator or by a single-line comment. The compiler has finished processing a valid preprocessor directive, and has encountered something that violates this syntax constraint. - -## Example - - The following sample generates CS1696. - -```csharp -// CS1696.cs -class Test -{ - public static void Main() - { - #pragma warning disable 1030;219 // CS1696 - #pragma warning disable 1030 // OK - } -} -``` diff --git a/docs/csharp/misc/cs1709.md b/docs/csharp/misc/cs1709.md deleted file mode 100644 index 87a9883e39347..0000000000000 --- a/docs/csharp/misc/cs1709.md +++ /dev/null @@ -1,30 +0,0 @@ ---- -description: "Learn more about: Compiler Warning (level 1) CS1709" -title: "Compiler Warning (level 1) CS1709" -ms.date: 07/20/2015 -f1_keywords: - - "CS1709" -helpviewer_keywords: - - "CS1709" -ms.assetid: e2bb1d30-4f30-4e10-82da-df1d3418454c ---- -# Compiler Warning (level 1) CS1709 - -Filename specified for preprocessor directive is empty - - You have specified a preprocessor directive that includes a file name, but that file is empty. To resolve this warning, put the needed content into the file. - -## Example - - The following example generates CS1709. - -```csharp -// CS1709.cs -class Test -{ - static void Main() - { - #pragma checksum "" "{406EA660-64CF-4C82-B6F0-42D48172A799}" "" // CS1709 - } -} -``` From 4011e2e041cc6b44f04a45cd41ce4aeee55d1f2a Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Tue, 7 Oct 2025 14:48:39 -0400 Subject: [PATCH 3/8] Add additional files I had copilot search for additional files that I had missed earlier in the appropriate folders. These are the ones it found. --- .github/prompts/error-consolidation.md | 2 + .openpublishing.redirection.csharp.json | 12 ++++ .../compiler-messages/preprocessor-errors.md | 72 +++++++++++++++++++ docs/csharp/language-reference/toc.yml | 2 +- docs/csharp/misc/cs1024.md | 22 ------ docs/csharp/misc/cs1025.md | 40 ----------- docs/csharp/misc/cs1030.md | 34 --------- 7 files changed, 87 insertions(+), 97 deletions(-) delete mode 100644 docs/csharp/misc/cs1024.md delete mode 100644 docs/csharp/misc/cs1025.md delete mode 100644 docs/csharp/misc/cs1030.md diff --git a/.github/prompts/error-consolidation.md b/.github/prompts/error-consolidation.md index 6ef92ab117db0..48c4832773ab3 100644 --- a/.github/prompts/error-consolidation.md +++ b/.github/prompts/error-consolidation.md @@ -1 +1,3 @@ Add the contents of cs1027.md to the preprocessor-errors.md file, sort that file by 'source_path_from_root'. Add a redirection for cs1027.md to point to preprocessor-errors.md, add "cs1027: to the list of display names in the TOC for preprocessor-errors.md. Finally, delete cs1027.md + +Search all files in the docs/csharp/language-reference/compiler-messages folder for any other errors and warnings that involve preprocessor tokens. Give me a list to review for possible additional consolidation \ No newline at end of file diff --git a/.openpublishing.redirection.csharp.json b/.openpublishing.redirection.csharp.json index b8af37ed1fbd0..976f2961e1071 100644 --- a/.openpublishing.redirection.csharp.json +++ b/.openpublishing.redirection.csharp.json @@ -1838,6 +1838,14 @@ "source_path_from_root": "/docs/csharp/misc/cs1020.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/overload-resolution" }, + { + "source_path_from_root": "/docs/csharp/misc/cs1024.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/preprocessor-errors" + }, + { + "source_path_from_root": "/docs/csharp/misc/cs1025.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/preprocessor-errors" + }, { "source_path_from_root": "/docs/csharp/misc/cs1027.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/preprocessor-errors" @@ -1850,6 +1858,10 @@ "source_path_from_root": "/docs/csharp/language-reference/compiler-messages/cs1029.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/preprocessor-errors" }, + { + "source_path_from_root": "/docs/csharp/misc/cs1030.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/preprocessor-errors" + }, { "source_path_from_root": "/docs/csharp/misc/cs1032.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/preprocessor-errors" diff --git a/docs/csharp/language-reference/compiler-messages/preprocessor-errors.md b/docs/csharp/language-reference/compiler-messages/preprocessor-errors.md index 547a453b992a9..f7217d1ce50d7 100644 --- a/docs/csharp/language-reference/compiler-messages/preprocessor-errors.md +++ b/docs/csharp/language-reference/compiler-messages/preprocessor-errors.md @@ -2,9 +2,12 @@ title: Errors and warnings generated by the C# preprocessor description: Learn how to diagnose and fix errors related to preprocessor tokens how they are used in C# source files. f1_keywords: + - "CS1024" + - "CS1025" - "CS1027" - "CS1028" - "CS1029" + - "CS1030" - "CS1032" - "CS1038" - "CS1040" @@ -26,9 +29,12 @@ f1_keywords: - "CS9299" - "CS9314" helpviewer_keywords: + - "CS1024" + - "CS1025" - "CS1027" - "CS1028" - "CS1029" + - "CS1030" - "CS1032" - "CS1038" - "CS1040" @@ -55,9 +61,12 @@ ms.date: 10/07/2025 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* @@ -79,6 +88,46 @@ The compiler generates the following errors for incorrect use of preprocessor di - **CS9299**: *`#:` directives cannot be after `#if` directive* - **CS9314**: *`#!` directives can be only used in scripts or file-based programs* +## CS1024: Preprocessor directive expected + +A line began with the pound symbol (#), but the subsequent string was not a valid [preprocessor directive](../preprocessor-directives.md). + +The following sample generates CS1024: + +```csharp +// CS1024.cs +#import System // CS1024 +``` + +## CS1025: Single-line comment or end-of-line expected + +A line with a [preprocessor directive](../preprocessor-directives.md) cannot have a multiline comment. + +The following sample generates CS1025: + +```csharp +#if true /* hello +*/ // CS1025 +#endif // this is a good comment +``` + +CS1025 could also occur if you attempt some invalid preprocessor directive, as follows: + +```csharp +// CS1025.cs +#define a + +class Sample +{ + static void Main() + { + #if a 1 // CS1025, invalid syntax + System.Console.WriteLine("Hello, World!"); + #endif + } +} +``` + ## CS1027: #endif directive expected A matching `#endif` [preprocessor directive](../preprocessor-directives.md#conditional-compilation) was not found for a specified `#if` directive. Or, the compiler may have found a `#endregion` directive when there was no matching `#region` directive inside a `#if` block. @@ -146,6 +195,29 @@ Compilation produces the following output: example.cs(9,8): error CS1029: #error: 'Let's give an error here // CS1029 ' ``` +## CS1030: #warning: 'text' + +Displays the text of a warning defined with the [#warning](../preprocessor-directives.md#error-and-warning-information) directive. + +The following sample shows how to create a user-defined warning: + +```csharp +// CS1030.cs +class Sample +{ + static void Main() + { + #warning Let's give a warning here + } +} +``` + +Compilation produces the following output: + +```console +example.cs(6,16): warning CS1030: #warning: 'Let's give a warning here' +``` + ## CS1032: Cannot define/undefine preprocessor symbols after first token in file The `#define` and `#undef` [preprocessor directives](../preprocessor-directives.md#defining-symbols) must be used at the beginning of a program, before any other keywords, such as those used in the namespace declaration. diff --git a/docs/csharp/language-reference/toc.yml b/docs/csharp/language-reference/toc.yml index 6d690dfe2644b..d1f1095ba1b29 100644 --- a/docs/csharp/language-reference/toc.yml +++ b/docs/csharp/language-reference/toc.yml @@ -463,7 +463,7 @@ items: - name: Preprocessor errors href: ./compiler-messages/preprocessor-errors.md displayName: > - CS1027, CS1028, CS1029, CS1032, CS1038, CS1040, CS1517, CS1560, CS1576, CS1578, CS1633, CS1634, CS1635, CS1691, CS1692, + CS1024, CS1025, CS1027, CS1028, CS1029, CS1030, CS1032, CS1038, CS1040, CS1517, CS1560, CS1576, CS1578, CS1633, CS1634, CS1635, CS1691, CS1692, CS1694, CS1695, CS1696, CS1709, CS9297, CS9298, CS9299, CS9314 - name: Feature or version missing href: ./compiler-messages/feature-version-errors.md diff --git a/docs/csharp/misc/cs1024.md b/docs/csharp/misc/cs1024.md deleted file mode 100644 index 8ebcdbc86cb3d..0000000000000 --- a/docs/csharp/misc/cs1024.md +++ /dev/null @@ -1,22 +0,0 @@ ---- -description: "Compiler Error CS1024" -title: "Compiler Error CS1024" -ms.date: 07/20/2015 -f1_keywords: - - "CS1024" -helpviewer_keywords: - - "CS1024" -ms.assetid: 41f587cb-1958-4eb6-9f8d-c03500e55e21 ---- -# Compiler Error CS1024 - -Preprocessor directive expected - - A line began with the pound symbol (#), but the subsequent string was not a valid [preprocessor directive](../language-reference/preprocessor-directives.md). - - The following sample generates CS1024: - -```csharp -// CS1024.cs -#import System // CS1024 -``` diff --git a/docs/csharp/misc/cs1025.md b/docs/csharp/misc/cs1025.md deleted file mode 100644 index a3a0cdb1b6786..0000000000000 --- a/docs/csharp/misc/cs1025.md +++ /dev/null @@ -1,40 +0,0 @@ ---- -description: "Compiler Error CS1025" -title: "Compiler Error CS1025" -ms.date: 07/20/2015 -f1_keywords: - - "CS1025" -helpviewer_keywords: - - "CS1025" -ms.assetid: 491c186f-cb40-47a9-9656-44fadfa18ae2 ---- -# Compiler Error CS1025 - -Single-line comment or end-of-line expected - - A line with a [preprocessor directive](../language-reference/preprocessor-directives.md) cannot have a multiline comment. - - The following sample generates CS1025: - -```csharp -#if true /* hello -*/ // CS1025 -#endif // this is a good comment -``` - - CS1025 could also occur if you attempt some invalid preprocessor directive, as follows: - -```csharp -// CS1025.cs -#define a - -class Sample -{ - static void Main() - { - #if a 1 // CS1025, invalid syntax - System.Console.WriteLine("Hello, World!"); - #endif - } -} -``` diff --git a/docs/csharp/misc/cs1030.md b/docs/csharp/misc/cs1030.md deleted file mode 100644 index 1db7f8596988c..0000000000000 --- a/docs/csharp/misc/cs1030.md +++ /dev/null @@ -1,34 +0,0 @@ ---- -description: "Compiler Warning (level 1) CS1030" -title: "Compiler Warning (level 1) CS1030" -ms.date: 08/20/2018 -f1_keywords: - - "CS1030" -helpviewer_keywords: - - "CS1030" -ms.assetid: 7c565abc-1366-4641-9383-ab8ba026ab96 ---- -# Compiler Warning (level 1) CS1030 - -\#warning: 'text' - - Displays the text of a warning defined with the [#warning](../language-reference/preprocessor-directives.md#error-and-warning-information) directive. - - The following sample shows how to create a user-defined warning: - -```csharp -// CS1030.cs -class Sample -{ - static void Main() - { - #warning Let's give a warning here - } -} -``` - -Compilation produces the following output: - -```console -example.cs(6,16): warning CS1030: #warning: 'Let's give a warning here' -``` From 298072fec5d79bcff0cc2485c97d8ddd2f5b3c84 Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Tue, 7 Oct 2025 15:15:14 -0400 Subject: [PATCH 4/8] Found a few more files searching In step 3, use the source from roslyn to find any additional errors and warnings that haven't been covered. --- .github/prompts/error-consolidation.md | 5 +- .../compiler-messages/nullable-warnings.md | 2 + .../compiler-messages/preprocessor-errors.md | 179 ++++++++++++++++++ ...n-t-have-specifics-on-this-csharp-error.md | 10 - 4 files changed, 185 insertions(+), 11 deletions(-) diff --git a/.github/prompts/error-consolidation.md b/.github/prompts/error-consolidation.md index 48c4832773ab3..01b64092f0542 100644 --- a/.github/prompts/error-consolidation.md +++ b/.github/prompts/error-consolidation.md @@ -1,3 +1,6 @@ Add the contents of cs1027.md to the preprocessor-errors.md file, sort that file by 'source_path_from_root'. Add a redirection for cs1027.md to point to preprocessor-errors.md, add "cs1027: to the list of display names in the TOC for preprocessor-errors.md. Finally, delete cs1027.md -Search all files in the docs/csharp/language-reference/compiler-messages folder for any other errors and warnings that involve preprocessor tokens. Give me a list to review for possible additional consolidation \ No newline at end of file +Search all files in the docs/csharp/language-reference/compiler-messages folder for any other errors and warnings that involve preprocessor tokens. Give me a list to review for possible additional consolidation. + +To make sure you've found all related errors, we'll check the source. Look in `CSharpResources.resx` for any elements where the `` element is a message related to preprocessor tokens. The symbolic constant for that value is in the `name` attribute on the parent `data` element. Find that value in `ErrorCodes.cs`. It will map to the compiler error code, where the code is "CS" followed by the number as a four digit number. Build a list of any not already added to preprocessor-errors.md + diff --git a/docs/csharp/language-reference/compiler-messages/nullable-warnings.md b/docs/csharp/language-reference/compiler-messages/nullable-warnings.md index 0d35943a6ce72..759115dea1396 100644 --- a/docs/csharp/language-reference/compiler-messages/nullable-warnings.md +++ b/docs/csharp/language-reference/compiler-messages/nullable-warnings.md @@ -98,6 +98,7 @@ helpviewer_keywords: - "CS8634" - "CS8636" - "CS8637" + - "CS8668" - "CS8639" - "CS8643" - "CS8644" @@ -214,6 +215,7 @@ The following warnings indicate that you haven't set the nullable context correc - **CS8632** - *The annotation for nullable reference types should only be used in code within a `#nullable` annotations context.* - **CS8636** - *Invalid option for `/nullable`; must be `disable`, `enable`, `warnings` or `annotations`* - **CS8637** - *Expected `enable`, `disable`, or `restore`* +- **CS8668** - *Expected `warnings`, `annotations`, or end of directive* To set the nullable context correctly, you have two options: diff --git a/docs/csharp/language-reference/compiler-messages/preprocessor-errors.md b/docs/csharp/language-reference/compiler-messages/preprocessor-errors.md index f7217d1ce50d7..04658ec92976f 100644 --- a/docs/csharp/language-reference/compiler-messages/preprocessor-errors.md +++ b/docs/csharp/language-reference/compiler-messages/preprocessor-errors.md @@ -24,6 +24,15 @@ f1_keywords: - "CS1695" - "CS1696" - "CS1709" + - "CS7009" + - "CS7010" + - "CS7011" + - "CS8097" + - "CS8098" + - "CS8938" + - "CS8939" + - "CS8996" + - "CS9028" - "CS9297" - "CS9298" - "CS9299" @@ -51,6 +60,15 @@ helpviewer_keywords: - "CS1695" - "CS1696" - "CS1709" + - "CS7009" + - "CS7010" + - "CS7011" + - "CS8097" + - "CS8098" + - "CS8938" + - "CS8939" + - "CS8996" + - "CS9028" - "CS9297" - "CS9298" - "CS9299" @@ -83,6 +101,15 @@ The compiler generates the following errors for incorrect use of preprocessor di - **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 `#if` directive* @@ -557,3 +584,155 @@ class Test } } ``` + +## CS7009: Cannot use #r after first token in file + +The [`#r` directive](../preprocessor-directives/preprocessor-r.md) can only be used before any tokens appear in a script file. After the first token, it's no longer allowed. + +The following example generates CS7009: + +```csharp +// CS7009.cs +using System; +#r "System.Collections" // CS7009 +``` + +## CS7010: Quoted file name expected + +A filename was expected in a [preprocessor directive](../preprocessor-directives.md), but it was not properly quoted. + +The following example generates CS7010: + +```csharp +// CS7010.cs +#r System.Collections.dll // CS7010 - missing quotes +``` + +To fix this error, enclose the filename in quotes: + +```csharp +#r "System.Collections.dll" // OK +``` + +## CS7011: #r is only allowed in scripts + +The [`#r` directive](../preprocessor-directives/preprocessor-r.md) can only be used in C# scripts (.csx files), not in regular C# source files. + +The following example generates CS7011: + +```csharp +// CS7011.cs +class Test +{ + static void Main() + { + #r "System.Collections" // CS7011 + } +} +``` + +## CS8097: #load is only allowed in scripts + +The [`#load` directive](../preprocessor-directives/preprocessor-load.md) can only be used in C# scripts (.csx files), not in regular C# source files. + +The following example generates CS8097: + +```csharp +// CS8097.cs +class Test +{ + static void Main() + { + #load "helper.csx" // CS8097 + } +} +``` + +## CS8098: Cannot use #load after first token in file + +The [`#load` directive](../preprocessor-directives/preprocessor-load.md) can only be used before any tokens appear in a script file. After the first token, it's no longer allowed. + +The following example generates CS8098: + +```csharp +// CS8098.csx +using System; +#load "helper.csx" // CS8098 +``` + +## CS8938: The #line directive value is missing or out of range + +The [`#line` directive](../preprocessor-directives/preprocessor-line.md) requires a valid line number. Line numbers must be between 1 and 16,707,566. + +The following example generates CS8938: + +```csharp +// CS8938.cs +#line // CS8938 - missing value +#line 0 // CS8938 - out of range +class Test { } +``` + +To fix this error, provide a valid line number: + +```csharp +#line 100 // OK +class Test { } +``` + +## CS8939: The #line directive end position must be greater than or equal to the start position + +When using the [`#line` directive](../preprocessor-directives/preprocessor-line.md) with span syntax, the end position must be greater than or equal to the start position. + +The following example generates CS8939: + +```csharp +// CS8939.cs +#line (1, 10) - (1, 5) "file.cs" // CS8939 - end column < start column +class Test { } +``` + +To fix this error, ensure the end position is not before the start position: + +```csharp +#line (1, 5) - (1, 10) "file.cs" // OK +class Test { } +``` + +## CS8996: Raw string literals are not allowed in preprocessor directives + +[Raw string literals](../../misc/raw-string-literals.md) cannot be used in [preprocessor directives](../preprocessor-directives.md). Use regular string literals instead. + +The following example generates CS8996: + +```csharp +// CS8996.cs +#pragma checksum """raw_string""" "{406EA660-64CF-4C82-B6F0-42D48172A799}" "hash" // CS8996 +class Test { } +``` + +To fix this error, use a regular string literal: + +```csharp +#pragma checksum "filename.cs" "{406EA660-64CF-4C82-B6F0-42D48172A799}" "hash" // OK +class Test { } +``` + +## CS9028: The #line span directive requires space before the first parenthesis, before the character offset, and before the file name + +The [`#line` directive](../preprocessor-directives/preprocessor-line.md) with span syntax requires proper spacing between its components. + +The following example generates CS9028: + +```csharp +// CS9028.cs +#line(1, 1) - (1, 10)"file.cs" // CS9028 - missing spaces +class Test { } +``` + +To fix this error, add the required spaces: + +```csharp +#line (1, 1) - (1, 10) "file.cs" // OK +class Test { } +``` diff --git a/docs/csharp/misc/sorry-we-don-t-have-specifics-on-this-csharp-error.md b/docs/csharp/misc/sorry-we-don-t-have-specifics-on-this-csharp-error.md index e47cead8bcc3a..3259c0f899a1f 100644 --- a/docs/csharp/misc/sorry-we-don-t-have-specifics-on-this-csharp-error.md +++ b/docs/csharp/misc/sorry-we-don-t-have-specifics-on-this-csharp-error.md @@ -105,9 +105,6 @@ f1_keywords: - "CS4036" - "CS7002" - "CS7006" - - "CS7009" - - "CS7010" - - "CS7011" - "CS7012" - "CS7013" - "CS7014" @@ -231,8 +228,6 @@ f1_keywords: - "CS8094" - "CS8095" - "CS8096" - - "CS8097" - - "CS8098" - "CS8099" - "CS8100" - "CS8101" @@ -378,7 +373,6 @@ f1_keywords: - "CS8664" - "CS8665" - "CS8666" - - "CS8668" - "CS8669" - "CS8700" - "CS8701" @@ -483,8 +477,6 @@ f1_keywords: - "CS8934" - "CS8935" - "CS8937" - - "CS8938" - - "CS8939" - "CS8940" - "CS8941" - "CS8942" @@ -513,7 +505,6 @@ f1_keywords: - "CS8986" - "CS8987" - "CS8989" - - "CS8996" - "CS8997" - "CS8998" - "CS8999" @@ -541,7 +532,6 @@ f1_keywords: - "CS9025" - "CS9026" - "CS9027" - - "CS9028" - "CS9029" - "CS9030" - "CS9031" From 682fa7e7b1f33ce2295648d54a7d57a2fc8e1845 Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Tue, 7 Oct 2025 16:42:32 -0400 Subject: [PATCH 5/8] Combine related errors Create combined sections for related errors and warnings. Provide links to relevant language-reference articles. --- .github/prompts/error-consolidation.md | 19 +- .../compiler-messages/preprocessor-errors.md | 633 +++++------------- docs/csharp/language-reference/toc.yml | 4 +- 3 files changed, 186 insertions(+), 470 deletions(-) diff --git a/.github/prompts/error-consolidation.md b/.github/prompts/error-consolidation.md index 01b64092f0542..1705f493ff0ee 100644 --- a/.github/prompts/error-consolidation.md +++ b/.github/prompts/error-consolidation.md @@ -1,6 +1,23 @@ +# Copilot prompts to consolidate error codes. + +## Add a single existing file into the new consolidated article. + Add the contents of cs1027.md to the preprocessor-errors.md file, sort that file by 'source_path_from_root'. Add a redirection for cs1027.md to point to preprocessor-errors.md, add "cs1027: to the list of display names in the TOC for preprocessor-errors.md. Finally, delete cs1027.md -Search all files in the docs/csharp/language-reference/compiler-messages folder for any other errors and warnings that involve preprocessor tokens. Give me a list to review for possible additional consolidation. +## Search for other related articles that may be missed. + +Search all files in the docs/csharp/language-reference/compiler-messages and the docs/csharp/misc folder for any other errors and warnings that involve preprocessor tokens. Give me a list to review for possible additional consolidation. + +## Final search in roslyn source To make sure you've found all related errors, we'll check the source. Look in `CSharpResources.resx` for any elements where the `` element is a message related to preprocessor tokens. The symbolic constant for that value is in the `name` attribute on the parent `data` element. Find that value in `ErrorCodes.cs`. It will map to the compiler error code, where the code is "CS" followed by the number as a four digit number. Build a list of any not already added to preprocessor-errors.md +## Build consolidated sections + +Examine the highlighted section. Other H2s with similar themes should be combined in a similar format. The recommendations to fix the errors should point to language reference articles that explain the impacted syntax. + +To do that, make a new H2 section for the theme. Remove all the H2s for the individual error codes that are part of that theme. Where applicable, the new H2 can include text or examples from the H2s you remove. + +The list of errors at the top of the file should remain in numerical order, so it's easy for readers to scan. Each impacted error code should now have a link to the anchor tag for the new section. Repeat the list in the new section, but without the anchors, as shown in the highlighted text. + +Start with CS1024 and other errors that indicate "Not a valid preprocessor directive" \ No newline at end of file diff --git a/docs/csharp/language-reference/compiler-messages/preprocessor-errors.md b/docs/csharp/language-reference/compiler-messages/preprocessor-errors.md index 04658ec92976f..8f40af05db119 100644 --- a/docs/csharp/language-reference/compiler-messages/preprocessor-errors.md +++ b/docs/csharp/language-reference/compiler-messages/preprocessor-errors.md @@ -74,136 +74,147 @@ helpviewer_keywords: - "CS9299" - "CS9314" ms.date: 10/07/2025 +ai-usage: ai-assisted --- # Preprocessor errors and warnings The compiler generates the following errors for incorrect use of preprocessor directives: +- [**CS1024**](#invalid-preprocessor-directive-syntax): *Preprocessor directive expected* +- [**CS1025**](#invalid-preprocessor-directive-syntax): *Single-line comment or end-of-line expected* +- [**CS1027**](#invalid-preprocessor-directive-syntax): *#endif directive expected* +- [**CS1028**](#invalid-preprocessor-directive-syntax): *Unexpected preprocessor directive* +- [**CS1029**](#error-and-warning-directive-errors): *#error: 'text'* +- [**CS1030**](#error-and-warning-directive-errors): *#warning: 'text'* +- **CS1032**: *Cannot define/undefine preprocessor symbols after first token in file* +- [**CS1038**](#invalid-preprocessor-directive-syntax): *#endregion directive expected* +- [**CS1040**](#invalid-preprocessor-directive-syntax): *Preprocessor directives must appear as the first non-white-space character on a line* +- [**CS1517**](#invalid-preprocessor-directive-syntax): *Invalid preprocessor expression* +- [**CS1560**](#line-and-file-directive-errors): *Invalid filename specified for preprocessor directive. Filename is too long or not a valid filename* +- [**CS1576**](#line-and-file-directive-errors): *The line number specified for #line directive is missing or invalid* +- [**CS1578**](#line-and-file-directive-errors): *Filename, single-line comment or end-of-line expected* +- [**CS1633**](#invalid-preprocessor-directive-syntax): *Unrecognized #pragma directive* +- [**CS1634**](#error-and-warning-directive-errors): *Expected disable or restore* +- [**CS1635**](#error-and-warning-directive-errors): *Cannot restore warning 'warning code' because it was disabled globally* +- [**CS1691**](#error-and-warning-directive-errors): *'number' is not a valid warning number* +- [**CS1692**](#error-and-warning-directive-errors): *Invalid number* +- [**CS1694**](#line-and-file-directive-errors): *Invalid filename specified for preprocessor directive. Filename is too long or not a valid filename* +- [**CS1695**](#line-and-file-directive-errors): *Invalid #pragma checksum syntax; should be #pragma checksum "filename" "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}" "XXXX..."* +- [**CS1696**](#invalid-preprocessor-directive-syntax): *Single-line comment or end-of-line expected* +- [**CS1709**](#line-and-file-directive-errors): *Filename specified for preprocessor directive is empty* +- [**CS7009**](#file-contains-scriptcs-directives): *Cannot use #r after first token in file* +- [**CS7010**](#file-contains-scriptcs-directives): *Quoted file name expected* +- [**CS7011**](#file-contains-scriptcs-directives): *#r is only allowed in scripts* +- [**CS8097**](#file-contains-scriptcs-directives): *#load is only allowed in scripts* +- [**CS8098**](#file-contains-scriptcs-directives): *Cannot use #load after first token in file* +- [**CS8938**](#line-and-file-directive-errors): *The #line directive value is missing or out of range* +- [**CS8939**](#line-and-file-directive-errors): *The #line directive end position must be greater than or equal to the start position* +- [**CS8996**](#invalid-preprocessor-directive-syntax): *Raw string literals are not allowed in preprocessor directives* +- [**CS9028**](#line-and-file-directive-errors): *The #line span directive requires space before the first parenthesis, before the character offset, and before the file name* +- [**CS9297**](#incorrect-use-of-file-based-apps-directives): *`#:` directives cannot be after first token in file* +- [**CS9298**](#incorrect-use-of-file-based-apps-directives): *`#:` directives can be only used in file-based programs (`-features:FileBasedProgram`)* +- [**CS9299**](#incorrect-use-of-file-based-apps-directives): *`#:` directives cannot be after `#if` directive* +- [**CS9314**](#incorrect-use-of-file-based-apps-directives): *`#!` 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* -- **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 `#if` directive* -- **CS9314**: *`#!` directives can be only used in scripts or file-based programs* - -## CS1024: Preprocessor directive expected -A line began with the pound symbol (#), but the subsequent string was not a valid [preprocessor directive](../preprocessor-directives.md). +These errors indicate that you've used invalid syntax for [preprocessor directives](../preprocessor-directives.md). Common causes include: -The following sample generates CS1024: +- 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 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:** ```csharp -// CS1024.cs -#import System // CS1024 +#import System // CS1024 - "import" is not a valid directive ``` -## CS1025: Single-line comment or end-of-line expected - -A line with a [preprocessor directive](../preprocessor-directives.md) cannot have a multiline comment. - -The following sample generates CS1025: - +**CS1025 example - Single-line comment or end-of-line expected:** ```csharp #if true /* hello -*/ // CS1025 -#endif // this is a good comment +*/ // CS1025 - multiline comment not allowed +#endif ``` -CS1025 could also occur if you attempt some invalid preprocessor directive, as follows: - +**CS1027 example - #endif directive expected:** ```csharp -// CS1025.cs -#define a - -class Sample -{ - static void Main() - { - #if a 1 // CS1025, invalid syntax - System.Console.WriteLine("Hello, World!"); - #endif - } -} +#if true // CS1027 - missing #endif +class Test { } ``` -## CS1027: #endif directive expected - -A matching `#endif` [preprocessor directive](../preprocessor-directives.md#conditional-compilation) was not found for a specified `#if` directive. Or, the compiler may have found a `#endregion` directive when there was no matching `#region` directive inside a `#if` block. - -The following sample generates CS1027: +**CS1028 example - Unexpected preprocessor directive:** +```csharp +#endif // CS1028 - no matching #if +``` +**CS1038 example - #endregion directive expected:** ```csharp -// CS1027.cs -#if true // CS1027, uncomment next line to resolve -// #endif +#region testing +class Test { } +// CS1038 - missing #endregion +``` -namespace x -{ - public class clx - { - public static void Main() - { - } - } -} +**CS1040 example - Preprocessor directives must appear as the first non-white-space character:** +```csharp +/* Comment */ #define X // CS1040 - directive not first on line ``` -## CS1028: Unexpected preprocessor directive +**CS1517 example - Invalid preprocessor expression:** +```csharp +#if 1 // CS1517 - numeric literals not allowed +#endif +#if ~symbol // CS1517 - bitwise operators not allowed +#endif +``` -A [preprocessor directive](../preprocessor-directives.md) was found but not expected. +**CS1633 example - Unrecognized #pragma directive:** +```csharp +#pragma unknown // CS1633 - "unknown" is not a valid pragma +``` -For example, a `#endif` was found with no preceding `#if`. +**CS8996 example - Raw string literals are not allowed in preprocessor directives:** +```csharp +// CS8996.cs +#pragma checksum """raw_string""" "{406EA660-64CF-4C82-B6F0-42D48172A799}" "hash" // CS8996 +class Test { } +``` -The following sample generates CS1028: +To fix this error, use a regular string literal: ```csharp -// CS1028.cs -#endif // CS1028, no matching #if -namespace x -{ - public class clx - { - public static void Main() - { - } - } -} +#pragma checksum "filename.cs" "{406EA660-64CF-4C82-B6F0-42D48172A799}" "hash" // OK +class Test { } ``` -## CS1029: #error: 'text' +To fix these errors, ensure your preprocessor directives follow the correct syntax rules described in the [preprocessor directives documentation](../preprocessor-directives.md). -Displays the text of an error defined with the [#error](../preprocessor-directives.md#error-and-warning-information) directive. +## #error and #warning directive errors -The following sample shows how to create a user-defined error: +- **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`](../preprocessor-directives.md#error-and-warning-information), [`#warning`](../preprocessor-directives.md#error-and-warning-information), and [`#pragma warning`](../preprocessor-directives.md#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: ```csharp // CS1029.cs @@ -222,11 +233,7 @@ Compilation produces the following output: example.cs(9,8): error CS1029: #error: 'Let's give an error here // CS1029 ' ``` -## CS1030: #warning: 'text' - -Displays the text of a warning defined with the [#warning](../preprocessor-directives.md#error-and-warning-information) directive. - -The following sample shows how to create a user-defined warning: +**CS1030** displays the text of a warning defined with the `#warning` directive: ```csharp // CS1030.cs @@ -245,189 +252,7 @@ Compilation produces the following output: example.cs(6,16): warning CS1030: #warning: 'Let's give a warning here' ``` -## CS1032: Cannot define/undefine preprocessor symbols after first token in file - -The `#define` and `#undef` [preprocessor directives](../preprocessor-directives.md#defining-symbols) must be used at the beginning of a program, before any other keywords, such as those used in the namespace declaration. - -The following sample generates CS1032: - -```csharp -// CS1032.cs -namespace x -{ - public class clx - { - #define a // CS1032, put before namespace - public static void Main() - { - } - } -} -``` - -## CS1038: #endregion directive expected - -A [#region](../preprocessor-directives.md#defining-regions) directive did not have a matching [#endregion](../preprocessor-directives.md#defining-regions) directive. - -The following sample generates CS1038: - -```csharp -// CS1038.cs -#region testing - -public class clx -{ - public static void Main() - { - } -} -// CS1038 -// uncomment the next line to resolve -// #endregion -``` - -## CS1040: Preprocessor directives must appear as the first non-white-space character on a line - -A [preprocessor directive](../preprocessor-directives.md) was found on a line and was not the first token on the line. A directive must be the first token on the line. - -The following sample generates CS1040: - -```csharp -// CS1040.cs -/* Define a symbol, X */ #define X // CS1040 - -// try the following two lines instead -// /* Define a symbol, X */ -// #define X - -public class MyClass -{ - public static void Main() - { - } -} -``` - -## CS1517: Invalid preprocessor expression - -The compiler encountered an invalid preprocessor expression. - -For more information, see [Preprocessor Directives](../preprocessor-directives.md). - -The following sample shows some valid and invalid preprocessor expressions: - -```csharp -// CS1517.cs -#if symbol // OK -#endif -#if !symbol // OK -#endif -#if (symbol) // OK -#endif -#if true // OK -#endif -#if false // OK -#endif -#if 1 // CS1517 -#endif -#if ~symbol // CS1517 -#endif -#if * // CS1517 -#endif - -class x -{ - public static void Main() - { - } -} -``` - -## CS1560: Invalid filename specified for preprocessor directive. Filename is too long or not a valid filename - -The file name that was specified with [#line](../preprocessor-directives.md#error-and-warning-information) exceeded _MAX_PATH (256 characters) or the line on which `#line` was found exceeded 2000 characters. - -## Example - -The following sample generates CS1560. - -```csharp -// cs1560.cs -using System; -class MyClass -{ - public static void Main() - { - Console.WriteLine("Normal line #1."); - #line 21 "MyFile1234567890MyFile1234567890MyFile1234567890MyFile1234567890MyFile1234567890MyFile1234567890MyFile1234567890MyFile1234567890MyFile1234567890MyFile1234567890MyFile1234567890MyFile1234567890MyFile1234567890MyFile1234567890MyFile1234567890MyFile1234567890.txt" // CS1560 - } -} -``` - -## CS1576: The line number specified for #line directive is missing or invalid - -The compiler detected an error with the value passed to the [#line](../preprocessor-directives.md#error-and-warning-information) directive. - -The following sample generates CS1576: - -```csharp -// CS1576.cs -public class MyClass -{ - static void Main() - { - #line "abc.sc" // CS1576 - // try the following line instead - //#line 101 "abc.sc" - intt i; // error will be reported on line 101 - } -} -``` - -## CS1578: Filename, single-line comment or end-of-line expected - -After a [#line](../preprocessor-directives.md#error-and-warning-information) directive, only a file name (in double quotation marks) or a single-line comment is allowed. - -The following sample generates CS1578: - -```csharp -// CS1578.cs -class MyClass -{ - static void Main() - { - #line 101 abc.cs // CS1578 - // try the following line instead - //#line 101 "abc.cs" - intt i; // error will be reported on line 101 - } -} -``` - -## CS1633: Unrecognized #pragma directive - -The pragma used was not one of the known pragmas supported by the C# compiler. To resolve this error, use only pragmas supported. - -The following sample generates CS1633: - -```csharp -// CS1633.cs -// compile with: /W:1 -#pragma unknown // CS1633 - -class C -{ - public static void Main() - { - } -} -``` - -## CS1634: Expected disable or restore - -This error occurs if a [#pragma warning](../preprocessor-directives.md#pragma-warning) clause is badly formed, such as if disable or restore was omitted. - -The following sample generates CS1634: +**CS1634** occurs when a `#pragma warning` clause is badly formed, such as when disable or restore is omitted: ```csharp // CS1634.cs @@ -445,11 +270,7 @@ class MyClass } ``` -## CS1635: Cannot restore warning 'warning code' because it was disabled globally - -This warning occurs if you use the `/nowarn` command line option or project setting to disable a warning for the entire compilation unit, but you use `#pragma warning restore` to attempt to restore that warning. To resolve this error, remove the `/nowarn` command line option or project setting, or remove the `#pragma warning restore` for any warnings you are disabling via the command line or project settings. For more information, see [#pragma warning](../preprocessor-directives.md#pragma-warning). - -The following sample generates CS1635: +**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: ```csharp // CS1635.cs @@ -466,16 +287,12 @@ class MyClass if (MyEnum.three == MyEnum.two) System.Console.WriteLine("Duplicate"); -#pragma warning restore 162 +#pragma warning restore 162 // CS1635 } } ``` -## CS1691: 'number' is not a valid warning number - -A number that was passed to the [#pragma warning](../preprocessor-directives.md#pragma-warning) preprocessor directive was not a valid warning number. Verify that the number represents a warning, not an error or another sequence of characters. - -The following example generates CS1691: +**CS1691** occurs when a number passed to the `#pragma warning` directive is not a valid warning number: ```csharp // CS1691.cs @@ -496,11 +313,7 @@ public class C } ``` -## CS1692: Invalid number - -A number of preprocessor directives, such as `#pragma` and `#line`, use numbers as parameters. One of these numbers is invalid because it is too big, in the wrong format, contains illegal characters, and so on. To correct this error, correct the number. - -The following example generates CS1692: +**CS1692** occurs when a number in preprocessor directives like `#pragma` and `#line` is invalid because it's too big, in the wrong format, or contains illegal characters: ```csharp // CS1692.cs @@ -517,222 +330,108 @@ class A } ``` -## CS1694: Invalid filename specified for preprocessor directive. Filename is too long or not a valid filename - -This warning occurs when using the `#pragma checksum` preprocessor directive. The file name specified is longer than 256 characters. To resolve this warning, use a shorter file name. - -The following sample generates CS1694: - -```csharp -// cs1694.cs -#pragma checksum "MyFile1234567890MyFile1234567890MyFile1234567890MyFile1234567890MyFile1234567890MyFile1234567890MyFile1234567890MyFile1234567890MyFile1234567890MyFile1234567890MyFile1234567890MyFile1234567890MyFile1234567890MyFile1234567890MyFile1234567890MyFile1234567890.txt" {00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F} // CS1694 -class MyClass {} -``` - -## CS1695: Invalid #pragma checksum syntax; should be #pragma checksum "filename" "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}" "XXXX..." - -You should rarely encounter this error since the checksum is generally inserted at run time if you are generating code by means of the Code Dom API. - -However, if you were to type in this `#pragma` statement and mistype either the GUID or checksum, you would get this error. The syntax checking by the compiler does not validate that you typed in a correct GUID, but it does check for the right number of digits and delimiters, and that the digits are hexadecimal. Likewise, it verifies that the checksum contains an even number of digits, and that the digits are hexadecimal. - -The following example generates CS1695: - -```csharp -// CS1695.cs - -#pragma checksum "12345" // CS1695 - -public class Test -{ - static void Main() - { - } -} -``` - -## CS1696: Single-line comment or end-of-line expected +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](../preprocessor-directives.md#error-and-warning-information) and [#pragma warning](../preprocessor-directives.md#pragma-warning). -The compiler requires a preprocessor directive to be followed by an end-of-line terminator or by a single-line comment. The compiler has finished processing a valid preprocessor directive, and has encountered something that violates this syntax constraint. - -The following sample generates CS1696: - -```csharp -// CS1696.cs -class Test -{ - public static void Main() - { - #pragma warning disable 1030;219 // CS1696 - #pragma warning disable 1030 // OK - } -} -``` - -## CS1709: Filename specified for preprocessor directive is empty - -You have specified a preprocessor directive that includes a file name, but that file is empty. To resolve this warning, put the needed content into the file. - -The following example generates CS1709: - -```csharp -// CS1709.cs -class Test -{ - static void Main() - { - #pragma checksum "" "{406EA660-64CF-4C82-B6F0-42D48172A799}" "" // CS1709 - } -} -``` - -## CS7009: Cannot use #r after first token in file - -The [`#r` directive](../preprocessor-directives/preprocessor-r.md) can only be used before any tokens appear in a script file. After the first token, it's no longer allowed. - -The following example generates CS7009: - -```csharp -// CS7009.cs -using System; -#r "System.Collections" // CS7009 -``` +## `#line` and file directive errors -## CS7010: Quoted file name expected +- **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* -A filename was expected in a [preprocessor directive](../preprocessor-directives.md), but it was not properly quoted. +These errors indicate incorrect usage of the [`#line` directive](../preprocessor-directives.md#error-and-warning-information) or file-related preprocessor directives. Common causes include: -The following example generates CS7010: +- 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 checksum` syntax (CS1695). +- Improper spacing in `#line` span directives (CS9028). +**CS1560/CS1694 example - Invalid filename specified:** ```csharp -// CS7010.cs -#r System.Collections.dll // CS7010 - missing quotes +#line 100 "MyFile1234567890MyFile1234567890MyFile1234567890MyFile1234567890MyFile1234567890MyFile1234567890MyFile1234567890MyFile1234567890MyFile1234567890MyFile1234567890MyFile1234567890MyFile1234567890MyFile1234567890MyFile1234567890MyFile1234567890MyFile1234567890.txt" // CS1560/CS1694 - filename too long ``` -To fix this error, enclose the filename in quotes: - +**CS1576 example - Line number missing or invalid:** ```csharp -#r "System.Collections.dll" // OK +#line "abc.sc" // CS1576 - missing line number +#line abc "file.cs" // CS1576 - invalid line number format ``` -## CS7011: #r is only allowed in scripts - -The [`#r` directive](../preprocessor-directives/preprocessor-r.md) can only be used in C# scripts (.csx files), not in regular C# source files. - -The following example generates CS7011: - +**CS1578 example - Filename, comment or end-of-line expected:** ```csharp -// CS7011.cs -class Test -{ - static void Main() - { - #r "System.Collections" // CS7011 - } -} +#line 101 abc.cs // CS1578 - filename not quoted ``` -## CS8097: #load is only allowed in scripts - -The [`#load` directive](../preprocessor-directives/preprocessor-load.md) can only be used in C# scripts (.csx files), not in regular C# source files. - -The following example generates CS8097: - +**CS1695 example - Invalid #pragma checksum syntax:** ```csharp -// CS8097.cs -class Test -{ - static void Main() - { - #load "helper.csx" // CS8097 - } -} +#pragma checksum "12345" // CS1695 - missing GUID and checksum ``` -## CS8098: Cannot use #load after first token in file - -The [`#load` directive](../preprocessor-directives/preprocessor-load.md) can only be used before any tokens appear in a script file. After the first token, it's no longer allowed. - -The following example generates CS8098: - +**CS1709 example - Empty filename:** ```csharp -// CS8098.csx -using System; -#load "helper.csx" // CS8098 +#pragma checksum "" "{406EA660-64CF-4C82-B6F0-42D48172A799}" "" // CS1709 - empty filename ``` -## CS8938: The #line directive value is missing or out of range - -The [`#line` directive](../preprocessor-directives/preprocessor-line.md) requires a valid line number. Line numbers must be between 1 and 16,707,566. - -The following example generates CS8938: - +**CS8938 example - #line directive value missing or out of range:** ```csharp -// CS8938.cs #line // CS8938 - missing value -#line 0 // CS8938 - out of range -class Test { } -``` - -To fix this error, provide a valid line number: - -```csharp -#line 100 // OK -class Test { } +#line 0 // CS8938 - out of range (must be 1-16,707,566) ``` -## CS8939: The #line directive end position must be greater than or equal to the start position - -When using the [`#line` directive](../preprocessor-directives/preprocessor-line.md) with span syntax, the end position must be greater than or equal to the start position. - -The following example generates CS8939: - +**CS8939 example - #line end position error:** ```csharp -// CS8939.cs #line (1, 10) - (1, 5) "file.cs" // CS8939 - end column < start column -class Test { } ``` -To fix this error, ensure the end position is not before the start position: - +**CS9028 example - #line span directive spacing:** ```csharp -#line (1, 5) - (1, 10) "file.cs" // OK -class Test { } +#line(1, 1) - (1, 10)"file.cs" // CS9028 - missing spaces ``` -## CS8996: Raw string literals are not allowed in preprocessor directives +To fix these errors, ensure your `#line` directives and file-related preprocessor directives follow the correct syntax as described in the [preprocessor directives documentation](../preprocessor-directives.md). -[Raw string literals](../../misc/raw-string-literals.md) cannot be used in [preprocessor directives](../preprocessor-directives.md). Use regular string literals instead. +## File contains script.cs directives -The following example generates CS8996: +The following errors indicate [script.cs](https://scriptcs.net) syntax in a compiled C# file: -```csharp -// CS8996.cs -#pragma checksum """raw_string""" "{406EA660-64CF-4C82-B6F0-42D48172A799}" "hash" // CS8996 -class Test { } -``` +- **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* -To fix this error, use a regular string literal: +These directives aren't supported in compiled C#. You must remove them, or use [script.cs](https://script.cs). -```csharp -#pragma checksum "filename.cs" "{406EA660-64CF-4C82-B6F0-42D48172A799}" "hash" // OK -class Test { } -``` +## Incorrect use of file-based apps 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 `#if` directive* +- **CS9314**: *`#!` directives can be only used in scripts or file-based programs* -The [`#line` directive](../preprocessor-directives/preprocessor-line.md) with span syntax requires proper spacing between its components. +These errors indicate that you've 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](../preprocessor-directives.md#file-based-apps) in the section on file-based apps. Or, you can explore file based apps by following the [tutorial](../../fundamentals/tutorials/file-based-programs.md) on file-based apps. -The following example generates CS9028: +## CS1032: Cannot define/undefine preprocessor symbols after first token in file -```csharp -// CS9028.cs -#line(1, 1) - (1, 10)"file.cs" // CS9028 - missing spaces -class Test { } -``` +The `#define` and `#undef` [preprocessor directives](../preprocessor-directives.md#defining-symbols) must be used at the beginning of a program, before any other keywords, such as those used in the namespace declaration. -To fix this error, add the required spaces: +The following sample generates CS1032: ```csharp -#line (1, 1) - (1, 10) "file.cs" // OK -class Test { } +// CS1032.cs +namespace x +{ + public class clx + { + #define a // CS1032, put before namespace + public static void Main() + { + } + } +} ``` diff --git a/docs/csharp/language-reference/toc.yml b/docs/csharp/language-reference/toc.yml index d1f1095ba1b29..a36c3d6996e81 100644 --- a/docs/csharp/language-reference/toc.yml +++ b/docs/csharp/language-reference/toc.yml @@ -463,8 +463,8 @@ items: - name: Preprocessor errors href: ./compiler-messages/preprocessor-errors.md displayName: > - CS1024, CS1025, CS1027, CS1028, CS1029, CS1030, CS1032, CS1038, CS1040, CS1517, CS1560, CS1576, CS1578, CS1633, CS1634, CS1635, CS1691, CS1692, - CS1694, CS1695, CS1696, CS1709, CS9297, CS9298, CS9299, CS9314 + CS1024, CS1025, CS1027, CS1028, CS1029, CS1030, CS1032, CS1038, CS1040, CS1517, CS1560, CS1576, CS1578, CS1633, CS1634, + CS1635, CS1691, CS1692, CS1694, CS1695, CS1696, CS1709, CS9297, CS9298, CS9299, CS9314 - name: Feature or version missing href: ./compiler-messages/feature-version-errors.md displayName: > From 1a9513298fefc4edb4d6fd258ba25e17cf61b028 Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Tue, 7 Oct 2025 16:51:47 -0400 Subject: [PATCH 6/8] build issues --- .../compiler-messages/preprocessor-errors.md | 17 +++++++++++++++++ .../preprocessor-directives.md | 4 ++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/docs/csharp/language-reference/compiler-messages/preprocessor-errors.md b/docs/csharp/language-reference/compiler-messages/preprocessor-errors.md index 8f40af05db119..4adcd15dc6118 100644 --- a/docs/csharp/language-reference/compiler-messages/preprocessor-errors.md +++ b/docs/csharp/language-reference/compiler-messages/preprocessor-errors.md @@ -140,11 +140,13 @@ These errors indicate that you've used invalid syntax for [preprocessor directiv - Using raw string literals in preprocessor directives (CS8996). **CS1024 example - Preprocessor directive expected:** + ```csharp #import System // CS1024 - "import" is not a valid directive ``` **CS1025 example - Single-line comment or end-of-line expected:** + ```csharp #if true /* hello */ // CS1025 - multiline comment not allowed @@ -152,17 +154,20 @@ These errors indicate that you've used invalid syntax for [preprocessor directiv ``` **CS1027 example - #endif directive expected:** + ```csharp #if true // CS1027 - missing #endif class Test { } ``` **CS1028 example - Unexpected preprocessor directive:** + ```csharp #endif // CS1028 - no matching #if ``` **CS1038 example - #endregion directive expected:** + ```csharp #region testing class Test { } @@ -170,11 +175,13 @@ class Test { } ``` **CS1040 example - Preprocessor directives must appear as the first non-white-space character:** + ```csharp /* Comment */ #define X // CS1040 - directive not first on line ``` **CS1517 example - Invalid preprocessor expression:** + ```csharp #if 1 // CS1517 - numeric literals not allowed #endif @@ -183,11 +190,13 @@ class Test { } ``` **CS1633 example - Unrecognized #pragma directive:** + ```csharp #pragma unknown // CS1633 - "unknown" is not a valid pragma ``` **CS8996 example - Raw string literals are not allowed in preprocessor directives:** + ```csharp // CS8996.cs #pragma checksum """raw_string""" "{406EA660-64CF-4C82-B6F0-42D48172A799}" "hash" // CS8996 @@ -353,43 +362,51 @@ These errors indicate incorrect usage of the [`#line` directive](../preprocessor - Improper spacing in `#line` span directives (CS9028). **CS1560/CS1694 example - Invalid filename specified:** + ```csharp #line 100 "MyFile1234567890MyFile1234567890MyFile1234567890MyFile1234567890MyFile1234567890MyFile1234567890MyFile1234567890MyFile1234567890MyFile1234567890MyFile1234567890MyFile1234567890MyFile1234567890MyFile1234567890MyFile1234567890MyFile1234567890MyFile1234567890.txt" // CS1560/CS1694 - filename too long ``` **CS1576 example - Line number missing or invalid:** + ```csharp #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:** + ```csharp #line 101 abc.cs // CS1578 - filename not quoted ``` **CS1695 example - Invalid #pragma checksum syntax:** + ```csharp #pragma checksum "12345" // CS1695 - missing GUID and checksum ``` **CS1709 example - Empty filename:** + ```csharp #pragma checksum "" "{406EA660-64CF-4C82-B6F0-42D48172A799}" "" // CS1709 - empty filename ``` **CS8938 example - #line directive value missing or out of range:** + ```csharp #line // CS8938 - missing value #line 0 // CS8938 - out of range (must be 1-16,707,566) ``` **CS8939 example - #line end position error:** + ```csharp #line (1, 10) - (1, 5) "file.cs" // CS8939 - end column < start column ``` **CS9028 example - #line span directive spacing:** + ```csharp #line(1, 1) - (1, 10)"file.cs" // CS9028 - missing spaces ``` diff --git a/docs/csharp/language-reference/preprocessor-directives.md b/docs/csharp/language-reference/preprocessor-directives.md index 46a9fc9cea794..6d6f0625fee15 100644 --- a/docs/csharp/language-reference/preprocessor-directives.md +++ b/docs/csharp/language-reference/preprocessor-directives.md @@ -299,7 +299,7 @@ You instruct the compiler to generate user-defined compiler errors and warnings, - `#warning`: Generate a compiler warning, with a specific message. - `#line`: Change the line number printed with compiler messages. -`#error` lets you generate a [CS1029](compiler-messages/cs1029.md) user-defined error from a specific location in your code. For example: +`#error` lets you generate a [CS1029](compiler-messages/preprocessor-errors.md) user-defined error from a specific location in your code. For example: ```csharp #error Deprecated code in this method. @@ -308,7 +308,7 @@ You instruct the compiler to generate user-defined compiler errors and warnings, > [!NOTE] > The compiler treats `#error version` in a special way and reports a compiler error, CS8304, with a message containing the used compiler and language versions. -`#warning` lets you generate a [CS1030](../misc/cs1030.md) level one compiler warning from a specific location in your code. For example: +`#warning` lets you generate a [CS1030](compiler-messages/preprocessor-errors.md) level one compiler warning from a specific location in your code. For example: ```csharp #warning Deprecated code in this method. From ee7bb383282ee745e61b02da0e01d304c3d4b46e Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Tue, 7 Oct 2025 17:12:57 -0400 Subject: [PATCH 7/8] proofread --- .../compiler-messages/preprocessor-errors.md | 52 +++++++------------ 1 file changed, 20 insertions(+), 32 deletions(-) diff --git a/docs/csharp/language-reference/compiler-messages/preprocessor-errors.md b/docs/csharp/language-reference/compiler-messages/preprocessor-errors.md index 4adcd15dc6118..cd864722f0f96 100644 --- a/docs/csharp/language-reference/compiler-messages/preprocessor-errors.md +++ b/docs/csharp/language-reference/compiler-messages/preprocessor-errors.md @@ -1,6 +1,6 @@ --- title: Errors and warnings generated by the C# preprocessor -description: Learn how to diagnose and fix errors related to preprocessor tokens how they are used in C# source files. +description: Learn how to diagnose and fix errors related to preprocessor tokens how they're used in C# source files. f1_keywords: - "CS1024" - "CS1025" @@ -86,7 +86,7 @@ The compiler generates the following errors for incorrect use of preprocessor di - [**CS1028**](#invalid-preprocessor-directive-syntax): *Unexpected preprocessor directive* - [**CS1029**](#error-and-warning-directive-errors): *#error: 'text'* - [**CS1030**](#error-and-warning-directive-errors): *#warning: 'text'* -- **CS1032**: *Cannot define/undefine preprocessor symbols after first token in file* +- [**CS1032**](#invalid-preprocessor-directive-syntax): *Cannot define/undefine preprocessor symbols after first token in file* - [**CS1038**](#invalid-preprocessor-directive-syntax): *#endregion directive expected* - [**CS1040**](#invalid-preprocessor-directive-syntax): *Preprocessor directives must appear as the first non-white-space character on a line* - [**CS1517**](#invalid-preprocessor-directive-syntax): *Invalid preprocessor expression* @@ -129,12 +129,13 @@ The compiler generates the following errors for incorrect use of preprocessor di - **CS1696**: *Single-line comment or end-of-line expected* - **CS8996**: *Raw string literals are not allowed in preprocessor directives* -These errors indicate that you've used invalid syntax for [preprocessor directives](../preprocessor-directives.md). Common causes include: +These errors indicate that you used invalid syntax for [preprocessor directives](../preprocessor-directives.md). 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). @@ -166,6 +167,13 @@ class Test { } #endif // CS1028 - no matching #if ``` +**CS1032 example - `#define` and `undef` preprocessor directives must appear before other tokens:** + +```csharp +/* Comment */ +#define X // CS1032 - directive not token in file +``` + **CS1038 example - #endregion directive expected:** ```csharp @@ -322,7 +330,7 @@ public class C } ``` -**CS1692** occurs when a number in preprocessor directives like `#pragma` and `#line` is invalid because it's too big, in the wrong format, or contains illegal characters: +**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: ```csharp // CS1692.cs @@ -413,17 +421,6 @@ These errors indicate incorrect usage of the [`#line` directive](../preprocessor To fix these errors, ensure your `#line` directives and file-related preprocessor directives follow the correct syntax as described in the [preprocessor directives documentation](../preprocessor-directives.md). -## File contains script.cs directives - -The following errors indicate [script.cs](https://scriptcs.net) 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](https://script.cs). - ## Incorrect use of file-based apps directives - **CS9297**: *`#:` directives cannot be after first token in file* @@ -431,24 +428,15 @@ These directives aren't supported in compiled C#. You must remove them, or use [ - **CS9299**: *`#:` directives cannot be after `#if` directive* - **CS9314**: *`#!` directives can be only used in scripts or file-based programs* -These errors indicate that you've 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](../preprocessor-directives.md#file-based-apps) in the section on file-based apps. Or, you can explore file based apps by following the [tutorial](../../fundamentals/tutorials/file-based-programs.md) on file-based apps. +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](../preprocessor-directives.md#file-based-apps) in the section on file-based apps. Or, you can explore file based apps by following the [tutorial](../../fundamentals/tutorials/file-based-programs.md) on file-based apps. -## CS1032: Cannot define/undefine preprocessor symbols after first token in file +## File contains script.cs directives -The `#define` and `#undef` [preprocessor directives](../preprocessor-directives.md#defining-symbols) must be used at the beginning of a program, before any other keywords, such as those used in the namespace declaration. +The following errors indicate [script.cs](https://scriptcs.net) syntax in a compiled C# file: -The following sample generates CS1032: +- **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* -```csharp -// CS1032.cs -namespace x -{ - public class clx - { - #define a // CS1032, put before namespace - public static void Main() - { - } - } -} -``` +These directives aren't supported in compiled C#. You must remove them, or use [script.cs](http://scriptcs.net/). From 7f1661782eb3022e77d461b7cf70a12eaa19bada Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Tue, 7 Oct 2025 17:18:20 -0400 Subject: [PATCH 8/8] Apply suggestions from code review Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- .../compiler-messages/preprocessor-errors.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/csharp/language-reference/compiler-messages/preprocessor-errors.md b/docs/csharp/language-reference/compiler-messages/preprocessor-errors.md index cd864722f0f96..8b8d03f377a41 100644 --- a/docs/csharp/language-reference/compiler-messages/preprocessor-errors.md +++ b/docs/csharp/language-reference/compiler-messages/preprocessor-errors.md @@ -167,11 +167,11 @@ class Test { } #endif // CS1028 - no matching #if ``` -**CS1032 example - `#define` and `undef` preprocessor directives must appear before other tokens:** +**CS1032 example - `#define` and `#undef` preprocessor directives must appear before other tokens:** ```csharp /* Comment */ -#define X // CS1032 - directive not token in file +#define X // CS1032 - directive not first token in file ``` **CS1038 example - #endregion directive expected:**