From 9ad1e414cc4bc7a6af3b35db39ea96adf44f0a6e Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Fri, 6 Feb 2026 15:28:45 -0500 Subject: [PATCH 01/12] Add new error codes --- .../UsingDirectives/FileScopedName.cs | 7 +++ .../using-directive-errors.md | 44 +++++++++++++++++++ docs/csharp/language-reference/toc.yml | 4 +- ...n-t-have-specifics-on-this-csharp-error.md | 3 -- 4 files changed, 53 insertions(+), 5 deletions(-) create mode 100644 docs/csharp/language-reference/compiler-messages/snippets/UsingDirectives/FileScopedName.cs diff --git a/docs/csharp/language-reference/compiler-messages/snippets/UsingDirectives/FileScopedName.cs b/docs/csharp/language-reference/compiler-messages/snippets/UsingDirectives/FileScopedName.cs new file mode 100644 index 0000000000000..6073b50aae1b3 --- /dev/null +++ b/docs/csharp/language-reference/compiler-messages/snippets/UsingDirectives/FileScopedName.cs @@ -0,0 +1,7 @@ +using System; + +namespace FileScopedNamespace; + +public class Sample +{ +} diff --git a/docs/csharp/language-reference/compiler-messages/using-directive-errors.md b/docs/csharp/language-reference/compiler-messages/using-directive-errors.md index c52b701cfc5c9..dcf98e6dcd931 100644 --- a/docs/csharp/language-reference/compiler-messages/using-directive-errors.md +++ b/docs/csharp/language-reference/compiler-messages/using-directive-errors.md @@ -20,6 +20,9 @@ f1_keywords: - "CS8914" - "CS8915" - "CS8933" + - "CS8954" + - "CS8955" + - "CS8956" - "CS9130" - "CS9131" - "CS9132" @@ -44,6 +47,9 @@ helpviewer_keywords: - "CS8914" - "CS8915" - "CS8933" + - "CS8954" + - "CS8955" + - "CS8956" - "CS9130" - "CS9131" - "CS9132" @@ -71,6 +77,9 @@ That's be design. The text closely matches the text of the compiler error / warn - [**CS8085**](#restrictions-on-using-aliases): *Error: A 'using static' directive cannot be used to declare an alias.* - [**CS8914**](#global-using-directive): *Error: A global using directive cannot be used in a namespace declaration.* - [**CS8915**](#global-using-directive): *Error: A global using directive must precede all non-global using directives.* +- [**CS8954**](#file-scoped-namespace): *Error: Source file can only contain one file-scoped namespace declaration.* +- [**CS8955**](#file-scoped-namespace): *Error: Source file can not contain both file-scoped and normal namespace declarations.* +- [**CS8956**](#file-scoped-namespace): *Error: File-scoped namespace must precede all other members in a file.* - [**CS9130**](#restrictions-on-using-aliases): *Error: Using alias cannot be a `ref` type.* - [**CS9131**](#restrictions-on-using-aliases): *Error: Only a using alias can be `unsafe`.* - [**CS9132**](#restrictions-on-using-aliases): *Error: Using alias cannot be a nullable reference type.* @@ -141,6 +150,41 @@ Any `global using` directives must precede any non-global `using` directives in Furthermore, a `static global using` directive can't reference a [file-local](../keywords/file.md) type. +## File-scoped namespace + +A [file-scoped namespace](../keywords/namespace.md) declaration sets the namespace for all types declared in a file. A file-scoped namespace declaration must follow `using` directives and precede any type or namespace declarations in the file: + +:::code language="csharp" source="./snippets/UsingDirectives/FileScopedName.cs"::: + +A file can contain only one file-scoped namespace declaration. Declaring multiple file-scoped namespaces produces **CS8954**: + +```csharp +namespace One; + +namespace Two; // CS8954 + +public class C { } +``` + +Furthermore, if a file contains a file-scoped namespace declaration, it can't contain any block-scoped namespace declarations. Using both in a single file produces **CS8955**: + +```csharp +namespace One; + +namespace Two // CS8955 +{ + public class C { } +} +``` + +Finally, the file-scoped namespace declaration must precede any type declarations in that file. Declaring types before the file-scoped namespace declaration produces **CS8956**: + +```csharp +public class C { } + +namespace One; // CS8956 +``` + ## Alias qualifier The alias qualifier, [`::`](../operators/namespace-alias-qualifier.md), precedes a namespace alias, or follows the `global` alias. If you use `::` where `.` should be used to separate elements of a fully qualified name, the compiler emits one of **CS0431**, **CS0432**, **CS0687**, **CS7000*, or **CS8083**. diff --git a/docs/csharp/language-reference/toc.yml b/docs/csharp/language-reference/toc.yml index 9777c43856cf1..dd5210eafc3eb 100644 --- a/docs/csharp/language-reference/toc.yml +++ b/docs/csharp/language-reference/toc.yml @@ -649,8 +649,8 @@ items: href: ./compiler-messages/using-directive-errors.md displayName: > CS0105, CS0138, CS0431, CS0432, CS0440, CS0576, CS0687, CS1529, CS1537, CS7000, - CS7007, CS8019, CS8083, CS8085, CS8914, CS8915, CS8933, CS9130, CS9131, - CS9132, CS9133, CS9162, CS9163 + CS7007, CS8019, CS8083, CS8085, CS8914, CS8915, CS8933, CS8954, CS8955, CS8956 + CS9130, CS9131, CS9132, CS9133, CS9162, CS9163 - name: Using statements and declarations href: ./compiler-messages/using-statement-declaration-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 01d3609a06a44..32ca66a22ecc0 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 @@ -427,9 +427,6 @@ f1_keywords: - "CS8935" - "CS8937" - "CS8940" - - "CS8954" - - "CS8955" - - "CS8956" - "CS8959" - "CS8960" - "CS8961" From b713bfbbace610c9d71e2a0697db37e62a7b4051 Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Fri, 6 Feb 2026 16:17:49 -0500 Subject: [PATCH 02/12] Consolidate existing articles. --- .openpublishing.redirection.csharp.json | 28 +++++++ .../using-directive-errors.md | 76 +++++++++++++++++++ docs/csharp/language-reference/toc.yml | 21 +---- docs/csharp/misc/cs0430.md | 43 ----------- docs/csharp/misc/cs0439.md | 34 --------- docs/csharp/misc/cs1671.md | 25 ------ docs/csharp/misc/cs1679.md | 31 -------- docs/csharp/misc/cs1681.md | 31 -------- docs/csharp/misc/cs1730.md | 35 --------- docs/csharp/misc/cs2034.md | 29 ------- 10 files changed, 108 insertions(+), 245 deletions(-) delete mode 100644 docs/csharp/misc/cs0430.md delete mode 100644 docs/csharp/misc/cs0439.md delete mode 100644 docs/csharp/misc/cs1671.md delete mode 100644 docs/csharp/misc/cs1679.md delete mode 100644 docs/csharp/misc/cs1681.md delete mode 100644 docs/csharp/misc/cs1730.md delete mode 100644 docs/csharp/misc/cs2034.md diff --git a/.openpublishing.redirection.csharp.json b/.openpublishing.redirection.csharp.json index 4706854645830..24141604fb163 100644 --- a/.openpublishing.redirection.csharp.json +++ b/.openpublishing.redirection.csharp.json @@ -1993,10 +1993,18 @@ "source_path_from_root": "/docs/csharp/misc/cs0431.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/using-directive-errors" }, + { + "source_path_from_root": "/docs/csharp/misc/cs0430.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/using-directive-errors" + }, { "source_path_from_root": "/docs/csharp/misc/cs0432.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/using-directive-errors" }, + { + "source_path_from_root": "/docs/csharp/misc/cs0439.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/using-directive-errors" + }, { "source_path_from_root": "/docs/csharp/misc/cs0440.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/using-directive-errors" @@ -2637,10 +2645,22 @@ "source_path_from_root": "/docs/csharp/misc/cs1670.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/params-arrays" }, + { + "source_path_from_root": "/docs/csharp/misc/cs1671.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/using-directive-errors" + }, { "source_path_from_root": "/docs/csharp/misc/cs1673.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/lambda-expression-errors#syntax-limitations-in-lambda-expressions" }, + { + "source_path_from_root": "/docs/csharp/misc/cs1679.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/using-directive-errors" + }, + { + "source_path_from_root": "/docs/csharp/misc/cs1681.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/using-directive-errors" + }, { "source_path_from_root": "/docs/csharp/language-reference/compiler-messages/cs1674.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/using-statement-declaration-errors#implementing-idisposable-and-iasyncdisposable" @@ -2677,6 +2697,10 @@ "source_path_from_root": "/docs/csharp/misc/cs1714.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/assembly-references" }, + { + "source_path_from_root": "/docs/csharp/misc/cs1730.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/using-directive-errors" + }, { "source_path_from_root": "/docs/csharp/misc/cs1920.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/array-declaration-errors" @@ -2725,6 +2749,10 @@ "source_path_from_root": "/docs/csharp/misc/cs1954.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/array-declaration-errors" }, + { + "source_path_from_root": "/docs/csharp/misc/cs2034.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/using-directive-errors" + }, { "source_path_from_root": "/docs/csharp/misc/cs3006.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/overload-resolution" diff --git a/docs/csharp/language-reference/compiler-messages/using-directive-errors.md b/docs/csharp/language-reference/compiler-messages/using-directive-errors.md index dcf98e6dcd931..20f5efeb501a5 100644 --- a/docs/csharp/language-reference/compiler-messages/using-directive-errors.md +++ b/docs/csharp/language-reference/compiler-messages/using-directive-errors.md @@ -5,13 +5,20 @@ ms.date: 11/02/2023 f1_keywords: - "CS0105" - "CS0138" + - "CS0430" - "CS0431" - "CS0432" + - "CS0439" - "CS0440" - "CS0576" - "CS0687" - "CS1529" - "CS1537" + - "CS1671" + - "CS1679" + - "CS1681" + - "CS1730" + - "CS2034" - "CS7000" - "CS7007" - "CS8019" @@ -32,13 +39,20 @@ f1_keywords: helpviewer_keywords: - "CS0105" - "CS0138" + - "CS0430" - "CS0431" - "CS0432" + - "CS0439" - "CS0440" - "CS0576" - "CS0687" - "CS1529" - "CS1537" + - "CS1671" + - "CS1679" + - "CS1681" + - "CS1730" + - "CS2034" - "CS7000" - "CS7007" - "CS8019" @@ -65,12 +79,19 @@ This article covers the following compiler errors: That's be design. The text closely matches the text of the compiler error / warning for SEO purposes. --> - [**CS0138**](#using-static-directive): *Error: A using namespace directive can only be applied to namespaces; 'type' is a type not a namespace.* +- [**CS0430**](#using-directive): *Error: The extern alias 'alias' was not specified in a /reference option.* - [**CS0431**](#alias-qualifier): *Error: Cannot use alias 'identifier' with `::` since the alias references a type. Use `.` instead*. - [**CS0432**](#alias-qualifier): *Error: Alias 'identifier' not found.* +- [**CS0439**](#using-directive): *Error: An extern alias declaration must precede all other elements defined in the namespace.* - [**CS0576**](#alias-name-conflicts): *Error: Namespace 'namespace' contains a definition conflicting with alias 'identifier'.* - [**CS0687**](#alias-qualifier): *Error: The namespace alias qualifier `::` always resolves to a type or namespace so is illegal here. Consider using `.` instead.* - [**CS1529**](#using-directive): *Error: A using clause must precede all other elements defined in the namespace except extern alias declarations.* - [**CS1537**](#alias-name-conflicts): *Error: The using alias 'alias' appeared previously in this namespace.* +- [**CS1671**](#file-scoped-namespace): *Error: A namespace declaration cannot have modifiers or attributes.* +- [**CS1679**](#using-directive): *Error: Invalid extern alias for '/reference'; 'identifier' is not a valid identifier.* +- [**CS1681**](#using-directive): *Error: You cannot redefine the global extern alias.* +- [**CS1730**](#using-directive): *Error: Assembly and module attributes must precede all other elements defined in a file except using clauses and extern alias declarations.* +- [**CS2034**](#using-directive): *Error: A /reference option that declares an extern alias can only have one filename. To specify multiple aliases or filenames, use multiple /reference options.* - [**CS7000**](#alias-qualifier): *Error: Unexpected use of an aliased name.* - [**CS7007**](#using-static-directive): *Error: A `using static` directive can only be applied to types. Consider a `using namespace` directive instead* - [**CS8083**](#alias-qualifier): *Error: An alias-qualified name is not an expression.* @@ -112,6 +133,53 @@ To fix this issue, move any `using` declarations to the top of the file or the t :::code language="csharp" source="./snippets/UsingDirectives/MyClass.cs" id="UsingExample"::: +Similarly, an [extern alias](../keywords/extern-alias.md) declaration must precede all `using` directives and other namespace elements. Placing an `extern alias` after a `using` directive produces **CS0439**: + +```csharp +using System; + +extern alias MyType; // CS0439 +``` + +Move the `extern alias` declaration before any `using` directives to fix this error. + +The compiler produces **CS0430** when an `extern alias` in your source code doesn't match an alias specified in a [**References**](../compiler-options/inputs.md#references) compiler option. Ensure the alias name matches the alias specified in the project reference or `/reference` option: + +```csharp +extern alias MyType; // CS0430 if MyType isn't specified as an alias +``` + +The compiler produces **CS1679** when the alias specified in a `/reference` option isn't a valid C# identifier. Ensure the alias name follows C# identifier naming rules: + +```csharp +// compile with: /reference:123$BadIdentifier%=System.dll +// CS1679: '123$BadIdentifier%' is not a valid identifier +``` + +The compiler produces **CS1681** if you attempt to redefine the `global` extern alias. The `global` alias is predefined to include all unaliased references and can't be redefined: + +```csharp +// compile with: /reference:global=System.dll +// CS1681: You cannot redefine the global extern alias +``` + +The compiler produces **CS2034** when a single `/reference` option declares multiple extern aliases or filenames. Each extern alias must be specified with its own `/reference` option: + +```csharp +// compile with: /r:A1=lib1.dll;A2=lib2.dll +// CS2034: use separate /reference options instead +// fix: /r:A1=lib1.dll /r:A2=lib2.dll +extern alias A1; +extern alias A2; +``` + +Assembly and module level [attributes](../../advanced-topics/reflection-and-attributes/index.md) must precede all other elements defined in a file, except `using` clauses and `extern alias` declarations. Placing these attributes after types or other declarations produces **CS1730**: + +```csharp +class Test { } +[assembly: System.CLSCompliant(true)] // CS1730 +``` + The compiler produces warning **CS8933**, **CS0105** or diagnostic **CS8019** for a duplicate `using` directive from a `using` or `global using` directive. You can remove any duplicates. Incorrectly combining a `using` directive with the `static`, `global`, or `unsafe` modifiers on a `using` directive are covered later in this article. @@ -185,6 +253,14 @@ public class C { } namespace One; // CS8956 ``` +The compiler produces **CS1671** if you apply modifiers or attributes to namespace declarations. Namespaces can't have access modifiers or attributes: + +```csharp +public namespace NS // CS1671 +{ +} +``` + ## Alias qualifier The alias qualifier, [`::`](../operators/namespace-alias-qualifier.md), precedes a namespace alias, or follows the `global` alias. If you use `::` where `.` should be used to separate elements of a fully qualified name, the compiler emits one of **CS0431**, **CS0432**, **CS0687**, **CS7000*, or **CS8083**. diff --git a/docs/csharp/language-reference/toc.yml b/docs/csharp/language-reference/toc.yml index dd5210eafc3eb..d0a44520b2051 100644 --- a/docs/csharp/language-reference/toc.yml +++ b/docs/csharp/language-reference/toc.yml @@ -648,9 +648,10 @@ items: - name: Using directive and aliases href: ./compiler-messages/using-directive-errors.md displayName: > - CS0105, CS0138, CS0431, CS0432, CS0440, CS0576, CS0687, CS1529, CS1537, CS7000, - CS7007, CS8019, CS8083, CS8085, CS8914, CS8915, CS8933, CS8954, CS8955, CS8956 - CS9130, CS9131, CS9132, CS9133, CS9162, CS9163 + CS0105, CS0138, CS0430, CS0431, CS0432, CS0439, CS0440, CS0576, CS0687, CS1529, + CS1537, CS1671, CS1679, CS1681, CS1730, CS2034, CS7000, CS7007, CS8019, CS8083, + CS8085, CS8914, CS8915, CS8933, CS8954, CS8955, CS8956, CS9130, CS9131, CS9132, + CS9133, CS9162, CS9163 - name: Using statements and declarations href: ./compiler-messages/using-statement-declaration-errors.md displayName: > @@ -1010,16 +1011,12 @@ items: href: ../misc/cs0426.md - name: CS0428 href: ../misc/cs0428.md - - name: CS0430 - href: ../misc/cs0430.md - name: CS0433 href: ./compiler-messages/cs0433.md - name: CS0434 href: ../misc/cs0434.md - name: CS0438 href: ../misc/cs0438.md - - name: CS0439 - href: ../misc/cs0439.md - name: CS0441 href: ../misc/cs0441.md - name: CS0442 @@ -1526,8 +1523,6 @@ items: href: ../misc/cs1664.md - name: CS1667 href: ../misc/cs1667.md - - name: CS1671 - href: ../misc/cs1671.md - name: CS1672 href: ../misc/cs1672.md - name: CS1675 @@ -1538,12 +1533,8 @@ items: href: ../misc/cs1677.md - name: CS1678 href: ../misc/cs1678.md - - name: CS1679 - href: ../misc/cs1679.md - name: CS1680 href: ../misc/cs1680.md - - name: CS1681 - href: ../misc/cs1681.md - name: CS1688 href: ../misc/cs1688.md - name: CS1689 @@ -1574,8 +1565,6 @@ items: href: ../misc/cs1728.md - name: CS1729 href: ./compiler-messages/cs1729.md - - name: CS1730 - href: ../misc/cs1730.md - name: CS1731 href: ../misc/cs1731.md - name: CS1732 @@ -1694,8 +1683,6 @@ items: href: ./compiler-messages/cs2032.md - name: CS2033 href: ../misc/cs2033.md - - name: CS2034 - href: ../misc/cs2034.md - name: CS2035 href: ../misc/cs2035.md - name: CS2036 diff --git a/docs/csharp/misc/cs0430.md b/docs/csharp/misc/cs0430.md deleted file mode 100644 index 8f2b0c945610c..0000000000000 --- a/docs/csharp/misc/cs0430.md +++ /dev/null @@ -1,43 +0,0 @@ ---- -description: "Compiler Error CS0430" -title: "Compiler Error CS0430" -ms.date: 07/20/2015 -f1_keywords: - - "CS0430" -helpviewer_keywords: - - "CS0430" -ms.assetid: b63c4f9a-b1cd-41d2-a02e-2ed0f177450f ---- -# Compiler Error CS0430 - -The extern alias 'alias' was not specified in a /reference option - - This error occurs when extern Alias is encountered but Alias was not specified as a reference on the command line. To resolve CS0430, compile with **/reference**. - -## Example 1 - -```csharp -// CS0430_a.cs -// compile with: /target:library -public class MyClass -{ - public static void M() { /*...*/ } -} -``` - -## Example 2 - - Compiling with **/reference:MyType=cs0430_a.dll** to refer to the DLL created in the previous sample resolves this error. The following sample generates CS0430. - -```csharp -// CS0430_b.cs -extern alias MyType; // CS0430 -public class Test -{ - public static void Main() { MyType::MyClass.M(); } -} -``` - -## See also - -- [extern alias](../language-reference/keywords/extern-alias.md) diff --git a/docs/csharp/misc/cs0439.md b/docs/csharp/misc/cs0439.md deleted file mode 100644 index e22d85aa6133b..0000000000000 --- a/docs/csharp/misc/cs0439.md +++ /dev/null @@ -1,34 +0,0 @@ ---- -description: "Compiler Error CS0439" -title: "Compiler Error CS0439" -ms.date: 07/20/2015 -f1_keywords: - - "CS0439" -helpviewer_keywords: - - "CS0439" -ms.assetid: 5cbac869-1b1b-45f9-98c8-38c17348035f ---- -# Compiler Error CS0439 - -An extern alias declaration must precede all other elements defined in the namespace - -This error occurs when an `extern` declaration comes after something else, such as a `using` declaration, in the same namespace. The `extern` declarations must come before all other namespace elements. - -## Example - -The following example generates CS0439: - -```csharp -// CS0439.cs -using System; - -extern alias MyType; // CS0439 -// To resolve the error, make the extern alias the first line in the file. - -public class Test -{ - public static void Main() - { - } -} -``` diff --git a/docs/csharp/misc/cs1671.md b/docs/csharp/misc/cs1671.md deleted file mode 100644 index 8e8abd541c283..0000000000000 --- a/docs/csharp/misc/cs1671.md +++ /dev/null @@ -1,25 +0,0 @@ ---- -description: "Learn more about: Compiler Error CS1671" -title: "Compiler Error CS1671" -ms.date: 07/20/2015 -f1_keywords: - - "CS1671" -helpviewer_keywords: - - "CS1671" -ms.assetid: 34255d2b-6ff6-4ac1-b617-3199e16726cf ---- -# Compiler Error CS1671 - -A namespace declaration cannot have modifiers or attributes - - Modifiers are not meaningful when applied to a namespace, so they are not allowed. - - The following sample generates CS1671: - -```csharp -// CS1671.cs -public namespace NS // CS1671 -{ - -} -``` diff --git a/docs/csharp/misc/cs1679.md b/docs/csharp/misc/cs1679.md deleted file mode 100644 index ddfb4e269365c..0000000000000 --- a/docs/csharp/misc/cs1679.md +++ /dev/null @@ -1,31 +0,0 @@ ---- -description: "Learn more about: Compiler Error CS1679" -title: "Compiler Error CS1679" -ms.date: 07/20/2015 -f1_keywords: - - "CS1679" -helpviewer_keywords: - - "CS1679" -ms.assetid: c42e9bca-212a-458e-88f8-b81c812436bb ---- -# Compiler Error CS1679 - -Invalid extern alias for '/reference'; 'identifier' is not a valid identifier - - When using the external assembly alias feature of the **/reference** option, the text that follows **/reference:** and that precedes the '=' must be a valid C# identifier or keyword according to the C# Language Specification. - - To correct this error, change text before the "=" to a valid C# identifier or keyword. - -## Example - - The following example generates CS1679. - -```csharp -// CS1679.cs -// compile with: /reference:123$BadIdentifier%=System.dll -class TestClass { - static void Main() - { - } -} -``` diff --git a/docs/csharp/misc/cs1681.md b/docs/csharp/misc/cs1681.md deleted file mode 100644 index e63029d8ffb28..0000000000000 --- a/docs/csharp/misc/cs1681.md +++ /dev/null @@ -1,31 +0,0 @@ ---- -description: "Learn more about: Compiler Error CS1681" -title: "Compiler Error CS1681" -ms.date: 07/20/2015 -f1_keywords: - - "CS1681" -helpviewer_keywords: - - "CS1681" -ms.assetid: 99934e15-1db8-4b71-9da8-a681a1d47407 ---- -# Compiler Error CS1681 - -You cannot redefine the global extern alias - - The global alias is already defined to include all unaliased references and therefore cannot be redefined. - -## Example - - The following sample generates CS1681. - -```csharp -// CS1681.cs -// compile with: /reference:global=System.dll -// CS1681 expected - -// try this instead: /reference:System.dll -class A -{ - static void Main() {} -} -``` diff --git a/docs/csharp/misc/cs1730.md b/docs/csharp/misc/cs1730.md deleted file mode 100644 index d206795dcf0a7..0000000000000 --- a/docs/csharp/misc/cs1730.md +++ /dev/null @@ -1,35 +0,0 @@ ---- -description: "Learn more about: Compiler Error CS1730" -title: "Compiler Error CS1730" -ms.date: 07/20/2015 -f1_keywords: - - "CS1730" -helpviewer_keywords: - - "CS1730" -ms.assetid: 20900ca0-702f-4f35-9a60-2dee9cb11902 ---- -# Compiler Error CS1730 - -Assembly and module attributes must precede all other elements defined in a file except using clauses and extern alias declarations. - - An attribute applied at the assembly level cannot appear after any type definitions. - -## To correct this error - -1. Move the attribute to the top of the file, but below the `using` directives and `extern` alias declarations. - -## Example - - The following code generates CS1730: - -```csharp -// cs1730.cs -class Test -{ -} -[assembly: System.Attribute] // CS1730 -``` - -## See also - -- [Attributes](/dotnet/csharp/advanced-topics/reflection-and-attributes) diff --git a/docs/csharp/misc/cs2034.md b/docs/csharp/misc/cs2034.md deleted file mode 100644 index 801cefdd18e39..0000000000000 --- a/docs/csharp/misc/cs2034.md +++ /dev/null @@ -1,29 +0,0 @@ ---- -description: "Learn more about: Compiler Error CS2034" -title: "Compiler Error CS2034" -ms.date: 07/20/2015 -f1_keywords: - - "CS2034" -helpviewer_keywords: - - "CS2034" -ms.assetid: 72f2b785-ee23-4a1b-b12d-42d19c324d5e ---- -# Compiler Error CS2034 - -A /reference option that declares an extern alias can only have one filename. To specify multiple aliases or filenames, use multiple /reference options. - - To specify two aliases and/or file names, use two **/reference** options, like this: - -## Example - - The following code will generate error CS2034. - -```csharp -// CS2034.cs -// compile with: /r:A1=cs2034a1.dll;A2=cs2034a2.dll -// to fix, compile with: /r:A1=cs2034a1.dll /r:A2=cs2034a2.dll -// CS2034 -extern alias A1; -extern alias A2; -using System; -``` From 9378afed22a0de9bd58d181004965be53857c09f Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Fri, 6 Feb 2026 16:32:07 -0500 Subject: [PATCH 03/12] Finish additional search --- .openpublishing.redirection.csharp.json | 4 +++ .../using-directive-errors.md | 6 +++++ docs/csharp/language-reference/toc.yml | 8 +++--- docs/csharp/misc/cs1680.md | 25 ------------------- ...n-t-have-specifics-on-this-csharp-error.md | 1 - 5 files changed, 13 insertions(+), 31 deletions(-) delete mode 100644 docs/csharp/misc/cs1680.md diff --git a/.openpublishing.redirection.csharp.json b/.openpublishing.redirection.csharp.json index 24141604fb163..a366fffbe5454 100644 --- a/.openpublishing.redirection.csharp.json +++ b/.openpublishing.redirection.csharp.json @@ -2657,6 +2657,10 @@ "source_path_from_root": "/docs/csharp/misc/cs1679.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/using-directive-errors" }, + { + "source_path_from_root": "/docs/csharp/misc/cs1680.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/using-directive-errors" + }, { "source_path_from_root": "/docs/csharp/misc/cs1681.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/using-directive-errors" diff --git a/docs/csharp/language-reference/compiler-messages/using-directive-errors.md b/docs/csharp/language-reference/compiler-messages/using-directive-errors.md index 20f5efeb501a5..96827ab9194f7 100644 --- a/docs/csharp/language-reference/compiler-messages/using-directive-errors.md +++ b/docs/csharp/language-reference/compiler-messages/using-directive-errors.md @@ -16,11 +16,13 @@ f1_keywords: - "CS1537" - "CS1671" - "CS1679" + - "CS1680" - "CS1681" - "CS1730" - "CS2034" - "CS7000" - "CS7007" + - "CS7015" - "CS8019" - "CS8083" - "CS8085" @@ -50,11 +52,13 @@ helpviewer_keywords: - "CS1537" - "CS1671" - "CS1679" + - "CS1680" - "CS1681" - "CS1730" - "CS2034" - "CS7000" - "CS7007" + - "CS7015" - "CS8019" - "CS8083" - "CS8085" @@ -89,11 +93,13 @@ That's be design. The text closely matches the text of the compiler error / warn - [**CS1537**](#alias-name-conflicts): *Error: The using alias 'alias' appeared previously in this namespace.* - [**CS1671**](#file-scoped-namespace): *Error: A namespace declaration cannot have modifiers or attributes.* - [**CS1679**](#using-directive): *Error: Invalid extern alias for '/reference'; 'identifier' is not a valid identifier.* +- [**CS1680**](#using-directive): *Error: Invalid reference alias option: 'alias=' -- missing filename.* - [**CS1681**](#using-directive): *Error: You cannot redefine the global extern alias.* - [**CS1730**](#using-directive): *Error: Assembly and module attributes must precede all other elements defined in a file except using clauses and extern alias declarations.* - [**CS2034**](#using-directive): *Error: A /reference option that declares an extern alias can only have one filename. To specify multiple aliases or filenames, use multiple /reference options.* - [**CS7000**](#alias-qualifier): *Error: Unexpected use of an aliased name.* - [**CS7007**](#using-static-directive): *Error: A `using static` directive can only be applied to types. Consider a `using namespace` directive instead* +- [**CS7015**](#using-directive): *Error: 'extern alias' is not valid in this context.* - [**CS8083**](#alias-qualifier): *Error: An alias-qualified name is not an expression.* - [**CS8085**](#restrictions-on-using-aliases): *Error: A 'using static' directive cannot be used to declare an alias.* - [**CS8914**](#global-using-directive): *Error: A global using directive cannot be used in a namespace declaration.* diff --git a/docs/csharp/language-reference/toc.yml b/docs/csharp/language-reference/toc.yml index d0a44520b2051..8d2ed14b8379c 100644 --- a/docs/csharp/language-reference/toc.yml +++ b/docs/csharp/language-reference/toc.yml @@ -649,9 +649,9 @@ items: href: ./compiler-messages/using-directive-errors.md displayName: > CS0105, CS0138, CS0430, CS0431, CS0432, CS0439, CS0440, CS0576, CS0687, CS1529, - CS1537, CS1671, CS1679, CS1681, CS1730, CS2034, CS7000, CS7007, CS8019, CS8083, - CS8085, CS8914, CS8915, CS8933, CS8954, CS8955, CS8956, CS9130, CS9131, CS9132, - CS9133, CS9162, CS9163 + CS1537, CS1671, CS1679, CS1680, CS1681, CS1730, CS2034, CS7000, CS7007, CS7015, + CS8019, CS8083, CS8085, CS8914, CS8915, CS8933, CS8954, CS8955, CS8956, CS9130, + CS9131, CS9132, CS9133, CS9162, CS9163 - name: Using statements and declarations href: ./compiler-messages/using-statement-declaration-errors.md displayName: > @@ -1533,8 +1533,6 @@ items: href: ../misc/cs1677.md - name: CS1678 href: ../misc/cs1678.md - - name: CS1680 - href: ../misc/cs1680.md - name: CS1688 href: ../misc/cs1688.md - name: CS1689 diff --git a/docs/csharp/misc/cs1680.md b/docs/csharp/misc/cs1680.md deleted file mode 100644 index 27ecbf93e70ab..0000000000000 --- a/docs/csharp/misc/cs1680.md +++ /dev/null @@ -1,25 +0,0 @@ ---- -description: "Learn more about: Compiler Error CS1680" -title: "Compiler Error CS1680" -ms.date: 07/20/2015 -f1_keywords: - - "CS1680" -helpviewer_keywords: - - "CS1680" -ms.assetid: 973da155-e6fa-4de8-94fd-7838f839a81f ---- -# Compiler Error CS1680 - -Invalid reference alias option: 'alias=' -- missing filename. - - This error occurs when you use the `alias` feature with the **/reference** compiler option without specifying a valid file name. - - The following sample generates CS1680. - -```csharp -// CS1680.cs -// compile with: /reference:alias= -// CS1680 expected -// To resolve, specify the name of a file with an assembly manifest -class MyClass {} -``` 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 32ca66a22ecc0..1d123e78f467f 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 @@ -80,7 +80,6 @@ f1_keywords: - "CS7006" - "CS7012" - "CS7013" - - "CS7015" - "CS7016" - "CS7017" - "CS7018" From 4f8341d94e2accfce8934311f9212cf9bcfa8303 Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Mon, 9 Feb 2026 10:50:36 -0500 Subject: [PATCH 04/12] Add issues for file scoped namespaces Fixes #51445 Add the diagnostics that currently have no documentation to this file. --- .../UsingDirectives/FileScopedName.cs | 7 - .../snippets/UsingDirectives/MyClass.cs | 8 - .../snippets/UsingDirectives/Program.cs | 21 -- .../UsingDirectives/UnsafeExamples.cs | 5 - .../UsingDirectives/UsingDirective.csproj | 11 - .../using-directive-errors.md | 243 +++++------------- docs/csharp/language-reference/toc.yml | 2 +- 7 files changed, 65 insertions(+), 232 deletions(-) delete mode 100644 docs/csharp/language-reference/compiler-messages/snippets/UsingDirectives/FileScopedName.cs delete mode 100644 docs/csharp/language-reference/compiler-messages/snippets/UsingDirectives/MyClass.cs delete mode 100644 docs/csharp/language-reference/compiler-messages/snippets/UsingDirectives/Program.cs delete mode 100644 docs/csharp/language-reference/compiler-messages/snippets/UsingDirectives/UnsafeExamples.cs delete mode 100644 docs/csharp/language-reference/compiler-messages/snippets/UsingDirectives/UsingDirective.csproj diff --git a/docs/csharp/language-reference/compiler-messages/snippets/UsingDirectives/FileScopedName.cs b/docs/csharp/language-reference/compiler-messages/snippets/UsingDirectives/FileScopedName.cs deleted file mode 100644 index 6073b50aae1b3..0000000000000 --- a/docs/csharp/language-reference/compiler-messages/snippets/UsingDirectives/FileScopedName.cs +++ /dev/null @@ -1,7 +0,0 @@ -using System; - -namespace FileScopedNamespace; - -public class Sample -{ -} diff --git a/docs/csharp/language-reference/compiler-messages/snippets/UsingDirectives/MyClass.cs b/docs/csharp/language-reference/compiler-messages/snippets/UsingDirectives/MyClass.cs deleted file mode 100644 index 6b7a9989b957e..0000000000000 --- a/docs/csharp/language-reference/compiler-messages/snippets/UsingDirectives/MyClass.cs +++ /dev/null @@ -1,8 +0,0 @@ -// -using System.Text.Json; -namespace UsingDirective; -public class MyClass -{ -} -// - diff --git a/docs/csharp/language-reference/compiler-messages/snippets/UsingDirectives/Program.cs b/docs/csharp/language-reference/compiler-messages/snippets/UsingDirectives/Program.cs deleted file mode 100644 index 1d68daf69d706..0000000000000 --- a/docs/csharp/language-reference/compiler-messages/snippets/UsingDirectives/Program.cs +++ /dev/null @@ -1,21 +0,0 @@ -// -global using System.Text; -// - -// -using static System.Console; -// - -// -using static unsafe UnsafeExamples.UnsafeType; -// - -// -using JSON = System.Text.Json; -using ValueMap = System.Collections.Generic.Dictionary; -using TimedData = (System.DateTime timeRecorded, decimal value); -// - -Console.WriteLine("Using directives"); - - diff --git a/docs/csharp/language-reference/compiler-messages/snippets/UsingDirectives/UnsafeExamples.cs b/docs/csharp/language-reference/compiler-messages/snippets/UsingDirectives/UnsafeExamples.cs deleted file mode 100644 index d3868d558803b..0000000000000 --- a/docs/csharp/language-reference/compiler-messages/snippets/UsingDirectives/UnsafeExamples.cs +++ /dev/null @@ -1,5 +0,0 @@ -namespace UnsafeExamples; -unsafe static class UnsafeType -{ - public static int* field; -} diff --git a/docs/csharp/language-reference/compiler-messages/snippets/UsingDirectives/UsingDirective.csproj b/docs/csharp/language-reference/compiler-messages/snippets/UsingDirectives/UsingDirective.csproj deleted file mode 100644 index 473cd32901075..0000000000000 --- a/docs/csharp/language-reference/compiler-messages/snippets/UsingDirectives/UsingDirective.csproj +++ /dev/null @@ -1,11 +0,0 @@ - - - - Exe - net8.0 - enable - enable - true - - - diff --git a/docs/csharp/language-reference/compiler-messages/using-directive-errors.md b/docs/csharp/language-reference/compiler-messages/using-directive-errors.md index 96827ab9194f7..69cf30b8faf69 100644 --- a/docs/csharp/language-reference/compiler-messages/using-directive-errors.md +++ b/docs/csharp/language-reference/compiler-messages/using-directive-errors.md @@ -1,7 +1,7 @@ --- title: "Resolve compiler errors and warnings related to using directives and using alias directives" description: "These errors and warnings indicate problems with using directives and using directive aliases. This information helps diagnose and fix those issues." -ms.date: 11/02/2023 +ms.date: 02/06/2026 f1_keywords: - "CS0105" - "CS0138" @@ -37,7 +37,6 @@ f1_keywords: - "CS9132" - "CS9133" - "CS9162" - - "CS9163" helpviewer_keywords: - "CS0105" - "CS0138" @@ -73,7 +72,6 @@ helpviewer_keywords: - "CS9132" - "CS9133" - "CS9162" - - "CS9163" --- # Resolve warnings related using namespaces @@ -87,10 +85,10 @@ That's be design. The text closely matches the text of the compiler error / warn - [**CS0431**](#alias-qualifier): *Error: Cannot use alias 'identifier' with `::` since the alias references a type. Use `.` instead*. - [**CS0432**](#alias-qualifier): *Error: Alias 'identifier' not found.* - [**CS0439**](#using-directive): *Error: An extern alias declaration must precede all other elements defined in the namespace.* -- [**CS0576**](#alias-name-conflicts): *Error: Namespace 'namespace' contains a definition conflicting with alias 'identifier'.* +- [**CS0576**](#using-alias-restrictions): *Error: Namespace 'namespace' contains a definition conflicting with alias 'identifier'.* - [**CS0687**](#alias-qualifier): *Error: The namespace alias qualifier `::` always resolves to a type or namespace so is illegal here. Consider using `.` instead.* - [**CS1529**](#using-directive): *Error: A using clause must precede all other elements defined in the namespace except extern alias declarations.* -- [**CS1537**](#alias-name-conflicts): *Error: The using alias 'alias' appeared previously in this namespace.* +- [**CS1537**](#using-alias-restrictions): *Error: The using alias 'alias' appeared previously in this namespace.* - [**CS1671**](#file-scoped-namespace): *Error: A namespace declaration cannot have modifiers or attributes.* - [**CS1679**](#using-directive): *Error: Invalid extern alias for '/reference'; 'identifier' is not a valid identifier.* - [**CS1680**](#using-directive): *Error: Invalid reference alias option: 'alias=' -- missing filename.* @@ -101,15 +99,15 @@ That's be design. The text closely matches the text of the compiler error / warn - [**CS7007**](#using-static-directive): *Error: A `using static` directive can only be applied to types. Consider a `using namespace` directive instead* - [**CS7015**](#using-directive): *Error: 'extern alias' is not valid in this context.* - [**CS8083**](#alias-qualifier): *Error: An alias-qualified name is not an expression.* -- [**CS8085**](#restrictions-on-using-aliases): *Error: A 'using static' directive cannot be used to declare an alias.* +- [**CS8085**](#using-alias-restrictions): *Error: A 'using static' directive cannot be used to declare an alias.* - [**CS8914**](#global-using-directive): *Error: A global using directive cannot be used in a namespace declaration.* - [**CS8915**](#global-using-directive): *Error: A global using directive must precede all non-global using directives.* - [**CS8954**](#file-scoped-namespace): *Error: Source file can only contain one file-scoped namespace declaration.* - [**CS8955**](#file-scoped-namespace): *Error: Source file can not contain both file-scoped and normal namespace declarations.* - [**CS8956**](#file-scoped-namespace): *Error: File-scoped namespace must precede all other members in a file.* -- [**CS9130**](#restrictions-on-using-aliases): *Error: Using alias cannot be a `ref` type.* -- [**CS9131**](#restrictions-on-using-aliases): *Error: Only a using alias can be `unsafe`.* -- [**CS9132**](#restrictions-on-using-aliases): *Error: Using alias cannot be a nullable reference type.* +- [**CS9130**](#using-alias-restrictions): *Error: Using alias cannot be a `ref` type.* +- [**CS9131**](#using-alias-restrictions): *Error: Only a using alias can be `unsafe`.* +- [**CS9132**](#using-alias-restrictions): *Error: Using alias cannot be a nullable reference type.* - [**CS9133**](#using-static-directive): *Error: `static` modifier must precede `unsafe` modifier.* - [**CS9162**](#using-static-directive): *Type is not valid for 'using static'. Only a class, struct, interface, enum, delegate, or namespace can be used.* @@ -120,212 +118,99 @@ And the following compiler warnings: - [**CS8019**](#using-directive): *Info: Unnecessary using directive.* - [**CS8933**](#using-directive): *Info: The using directive appeared previously as global using.* -These errors and warnings indicate you're `using` directive isn't formed correctly. The following sections cover these errors and how to correct them. +These errors and warnings indicate your `using` directive isn't formed correctly. The following sections cover these errors and how to correct them. ## Using directive -The `using` directive must precede any other elements in a `namespace` declaration, or before any `namespace` declarations in the file. Putting a `using` directive later in the file causes the compiler to produce error **CS1529**: +The following errors relate to `using` directives: -```csharp -namespace UsingDirective; -public class MyClass -{ -} +- **CS0105**: *The using directive for 'namespace' appeared previously in this namespace.* +- **CS0430**: *The extern alias 'alias' was not specified in a /reference option.* +- **CS0439**: *An extern alias declaration must precede all other elements defined in the namespace.* +- **CS1529**: *A using clause must precede all other elements defined in the namespace except extern alias declarations.* +- **CS1679**: *Invalid extern alias for '/reference'; 'identifier' is not a valid identifier.* +- **CS1680**: *Invalid reference alias option: 'alias=' -- missing filename.* +- **CS1681**: *You cannot redefine the global extern alias.* +- **CS1730**: *Assembly and module attributes must precede all other elements defined in a file except using clauses and extern alias declarations.* +- **CS2034**: *A /reference option that declares an extern alias can only have one filename. To specify multiple aliases or filenames, use multiple /reference options.* +- **CS7015**: *'extern alias' is not valid in this context.* +- **CS8019**: *Unnecessary using directive.* +- **CS8933**: *The using directive appeared previously as global using.* -using System.Text.Json; // CS1529 -``` +See the [using directive](../keywords/using-directive.md) and [extern alias](../keywords/extern-alias.md) language reference for usage rules. -To fix this issue, move any `using` declarations to the top of the file or the top of the namespace: +Move all `using` directives to the top of the file, or to the top of the namespace declaration, because the C# language requires `using` directives to come before other elements in a namespace (**CS1529**). Move all `extern alias` declarations before any `using` directives, because the language requires extern aliases to come before all other elements including `using` directives (**CS0439**, **CS7015**). Move all assembly and module level attributes after `using` clauses and `extern alias` declarations but before any type declarations, because attributes must follow directives but precede types (**CS1730**). -:::code language="csharp" source="./snippets/UsingDirectives/MyClass.cs" id="UsingExample"::: +Ensure that every `extern alias` declaration in your source code has a corresponding alias defined in your project's [reference options](../compiler-options/inputs.md#references), because the compiler can't resolve an alias that wasn't specified (**CS0430**). Use a separate `/reference` option for each extern alias rather than combining multiple aliases in a single option, because the compiler requires one alias per reference option (**CS2034**). Ensure the alias in your `/reference` option is a valid C# identifier, because the alias must follow identifier naming rules (**CS1679**). Include a filename after the `=` sign in your alias reference option, because the compiler needs to know which assembly the alias refers to (**CS1680**). Don't attempt to redefine the `global` extern alias, because `global` is a predefined alias that refers to all unaliased references (**CS1681**). -Similarly, an [extern alias](../keywords/extern-alias.md) declaration must precede all `using` directives and other namespace elements. Placing an `extern alias` after a `using` directive produces **CS0439**: - -```csharp -using System; - -extern alias MyType; // CS0439 -``` - -Move the `extern alias` declaration before any `using` directives to fix this error. - -The compiler produces **CS0430** when an `extern alias` in your source code doesn't match an alias specified in a [**References**](../compiler-options/inputs.md#references) compiler option. Ensure the alias name matches the alias specified in the project reference or `/reference` option: - -```csharp -extern alias MyType; // CS0430 if MyType isn't specified as an alias -``` - -The compiler produces **CS1679** when the alias specified in a `/reference` option isn't a valid C# identifier. Ensure the alias name follows C# identifier naming rules: - -```csharp -// compile with: /reference:123$BadIdentifier%=System.dll -// CS1679: '123$BadIdentifier%' is not a valid identifier -``` - -The compiler produces **CS1681** if you attempt to redefine the `global` extern alias. The `global` alias is predefined to include all unaliased references and can't be redefined: - -```csharp -// compile with: /reference:global=System.dll -// CS1681: You cannot redefine the global extern alias -``` - -The compiler produces **CS2034** when a single `/reference` option declares multiple extern aliases or filenames. Each extern alias must be specified with its own `/reference` option: - -```csharp -// compile with: /r:A1=lib1.dll;A2=lib2.dll -// CS2034: use separate /reference options instead -// fix: /r:A1=lib1.dll /r:A2=lib2.dll -extern alias A1; -extern alias A2; -``` - -Assembly and module level [attributes](../../advanced-topics/reflection-and-attributes/index.md) must precede all other elements defined in a file, except `using` clauses and `extern alias` declarations. Placing these attributes after types or other declarations produces **CS1730**: - -```csharp -class Test { } -[assembly: System.CLSCompliant(true)] // CS1730 -``` - -The compiler produces warning **CS8933**, **CS0105** or diagnostic **CS8019** for a duplicate `using` directive from a `using` or `global using` directive. You can remove any duplicates. - -Incorrectly combining a `using` directive with the `static`, `global`, or `unsafe` modifiers on a `using` directive are covered later in this article. +Remove duplicate `using` directives, because the compiler warns when the same namespace is imported multiple times (**CS0105**, **CS8019**, **CS8933**). ## Using static directive -The `using static` directive imports one type's members into the current namespace. The following example imports the methods from `System.Console`, such as `WriteLine` into the current namespace: - -:::code language="csharp" source="./snippets/UsingDirectives/Program.cs" id="UsingStatic"::: - -The compiler generates **CS0138** if you omit the `static` modifier: - -```csharp -using System.Console; // CS0138 -``` - -The compiler generates **CS7007** if you include the `static` modifier importing namespace instead of a type: +The following errors relate to `using static` directives: -```csharp -using static System; // CS7007 -``` +- **CS0138**: *A using namespace directive can only be applied to namespaces; 'type' is a type not a namespace.* +- **CS7007**: *A `using static` directive can only be applied to types. Consider a `using namespace` directive instead.* +- **CS9133**: *`static` modifier must precede `unsafe` modifier.* +- **CS9162**: *Type is not valid for 'using static'. Only a class, struct, interface, enum, delegate, or namespace can be used.* -The compiler emits CS9162 if the symbol isn't one of the proper types. +See the [using static directive](../keywords/using-directive.md#the-using-static-modifier) language reference for usage rules. -If you combine the `static` modifier with the `unsafe` modifier in a `using` directive, the `static` modifier must come first: - -:::code language="csharp" source="./snippets/UsingDirectives/Program.cs" id="UsingUnsafeStatic"::: +Add the `static` modifier when importing a type's members directly, because omitting `static` tells the compiler you're importing a namespace rather than a type (**CS0138**). Remove the `static` modifier when importing a namespace, because `using static` can only be applied to types, not namespaces (**CS7007**). Ensure the target of a `using static` directive is a class, struct, interface, enum, or delegate, because other types aren't valid targets for static imports (**CS9162**). Place the `static` modifier before the `unsafe` modifier when combining both, because the language requires modifiers in a specific order (**CS9133**). ## Global using directive -A `global using` directive imports the namespace or type in all source files in the current project: +The following errors relate to `global using` directives: -:::code language="csharp" source="./snippets/UsingDirectives/Program.cs" id="GlobalUsing"::: +- **CS8914**: *A global using directive cannot be used in a namespace declaration.* +- **CS8915**: *A global using directive must precede all non-global using directives.* -Any `global using` directives must precede any non-global `using` directives in that source file, and must not be placed in a `namespace`. Doing so results in **CS8915** and **CS8914**, respectively. +See the [global using directive](../keywords/using-directive.md#global-modifier) language reference for usage rules. -Furthermore, a `static global using` directive can't reference a [file-local](../keywords/file.md) type. +Move `global using` directives outside of any namespace declaration to file scope, because global usings apply project-wide and can't be scoped to a namespace (**CS8914**). Place all `global using` directives before any non-global `using` directives in the file, because the language requires global directives to precede local ones (**CS8915**). Note that a `static global using` directive can't reference a [file-local](../keywords/file.md) type. ## File-scoped namespace -A [file-scoped namespace](../keywords/namespace.md) declaration sets the namespace for all types declared in a file. A file-scoped namespace declaration must follow `using` directives and precede any type or namespace declarations in the file: - -:::code language="csharp" source="./snippets/UsingDirectives/FileScopedName.cs"::: - -A file can contain only one file-scoped namespace declaration. Declaring multiple file-scoped namespaces produces **CS8954**: - -```csharp -namespace One; - -namespace Two; // CS8954 - -public class C { } -``` - -Furthermore, if a file contains a file-scoped namespace declaration, it can't contain any block-scoped namespace declarations. Using both in a single file produces **CS8955**: - -```csharp -namespace One; - -namespace Two // CS8955 -{ - public class C { } -} -``` - -Finally, the file-scoped namespace declaration must precede any type declarations in that file. Declaring types before the file-scoped namespace declaration produces **CS8956**: +The following errors relate to file-scoped namespaces: -```csharp -public class C { } +- **CS1671**: *A namespace declaration cannot have modifiers or attributes.* +- **CS8954**: *Source file can only contain one file-scoped namespace declaration.* +- **CS8955**: *Source file can not contain both file-scoped and normal namespace declarations.* +- **CS8956**: *File-scoped namespace must precede all other members in a file.* -namespace One; // CS8956 -``` +See the [file-scoped namespace](../keywords/namespace.md) language reference for usage rules. -The compiler produces **CS1671** if you apply modifiers or attributes to namespace declarations. Namespaces can't have access modifiers or attributes: - -```csharp -public namespace NS // CS1671 -{ -} -``` +Use only one file-scoped namespace declaration per file, because the language allows only a single file-scoped namespace to set the namespace for all types in a file (**CS8954**). Choose either file-scoped or block-scoped namespace declarations within a single file, because the language doesn't allow mixing both styles (**CS8955**). Move the file-scoped namespace declaration before any type declarations, because the namespace must be established before types are declared (**CS8956**). Remove any access modifiers or attributes from namespace declarations, because namespaces can't have modifiers or attributes applied to them (**CS1671**). ## Alias qualifier -The alias qualifier, [`::`](../operators/namespace-alias-qualifier.md), precedes a namespace alias, or follows the `global` alias. If you use `::` where `.` should be used to separate elements of a fully qualified name, the compiler emits one of **CS0431**, **CS0432**, **CS0687**, **CS7000*, or **CS8083**. - -In all cases, replace the `::` with the `.` separator. - -In addition, if you define an alias named `global`, the compiler issues **CS0440**. The `global` alias always refers to the global namespace. Declaring an alias for it doesn't work, and you should pick a different name for your alias. - -## Alias name conflicts - -You can declare an [alias](../keywords/using-directive.md#the-using-alias) to a namespace or a type with a `using` directive: - -:::code language="csharp" source="./snippets/UsingDirectives/Program.cs" id="UsingAlias"::: - -You should try to create a unique name for the alias, the name on the left of the `=` sign in the preceding examples. Using a name that already maps to a Type (for example `Object`) or a namespace (`System`) can cause **CS0576** or **CS1537**. - -## Restrictions on using aliases - -Prior to C# 12, the language imposed these restrictions on `using` directives that create an alias for a type declaration: +The following errors relate to the alias qualifier: -- You can't create an alias with a `using static` directive: +- **CS0431**: *Cannot use alias 'identifier' with `::` since the alias references a type. Use `.` instead.* +- **CS0432**: *Alias 'identifier' not found.* +- **CS0440**: *Defining an alias named `global` is ill-advised since `global::` always references the global namespace and not an alias.* +- **CS0687**: *The namespace alias qualifier `::` always resolves to a type or namespace so is illegal here. Consider using `.` instead.* +- **CS7000**: *Unexpected use of an aliased name.* +- **CS8083**: *An alias-qualified name is not an expression.* - ```csharp - using static con = System.Console; - using static unsafe ip = int*; - ``` +See the [namespace alias qualifier](../operators/namespace-alias-qualifier.md) language reference for usage rules. -Beginning with C# 12, these restrictions are introduced: +Replace the `::` operator with the `.` operator when you access members of a type alias, because the `::` qualifier is only valid for namespace aliases, not type aliases (**CS0431**, **CS0687**). Ensure the alias you're referencing is declared by using a `using` directive or `extern alias`, because the compiler can't resolve an undefined alias (**CS0432**). Use the alias qualifier only in contexts where a type or namespace name is expected, because alias-qualified names aren't valid as expressions (**CS7000**, **CS8083**). Choose a different name for your alias instead of `global`, because `global` is reserved to refer to the global namespace and can't be redefined (**CS0440**). -- You can't use the `in`, `ref`, or `out` modifiers in a using alias: +## Using alias restrictions - ```csharp - // All these are invalid - using RefInt = ref int; - using OutInt = out int; - using InInt = in int; - ``` +The following errors relate to restrictions on using aliases: -- An `unsafe using` directive must specify an alias, or a `static using`: +- **CS0576**: *Namespace 'namespace' contains a definition conflicting with alias 'identifier'.* +- **CS1537**: *The using alias 'alias' appeared previously in this namespace.* +- **CS8085**: *A 'using static' directive cannot be used to declare an alias.* +- **CS9130**: *Using alias cannot be a `ref` type.* +- **CS9131**: *Only a using alias can be `unsafe`.* +- **CS9132**: *Using alias cannot be a nullable reference type.* - ```csharp - // Elsewhere: - public namespace UnsafeExamples - { - public unsafe static class UnsafeType - { - // ... - } - } +See the [using alias](../keywords/using-directive.md#the-using-alias) language reference for usage rules. - // Using directives: - using unsafe IntPointer = int*; - using static unsafe UnsafeExamples.UnsafeType; - using unsafe UnsafeExamples; // not allowed - ``` +Choose a unique name for your alias that doesn't conflict with existing type or namespace names in scope, because the compiler can't distinguish between the alias and the existing definition (**CS0576**). Use each alias name only once within a namespace, because duplicate alias declarations create ambiguity (**CS1537**). Remove the `static` modifier when declaring an alias, because aliases and static imports are mutually exclusive - use either `using static` to import members or `using Alias =` to create an alias, but not both together (**CS8085**). -- You can't create an alias to a nullable reference type: +Starting with C# 12, the following restrictions apply to using aliases: Don't use `ref`, `in`, or `out` modifiers in a using alias, because these parameter modifiers aren't valid in type alias contexts (**CS9130**). Use the `unsafe` modifier only with aliases that reference pointer types or with `using static` directives, because `unsafe` without an alias or static import isn't permitted (**CS9131**). Use a non-nullable reference type when creating an alias to a reference type, because nullable reference types can't be aliased directly (**CS9132**). - ```csharp - using NullableInt = System.Int32?; // Allowed - using NullableString = System.String?; // Not allowed - ``` diff --git a/docs/csharp/language-reference/toc.yml b/docs/csharp/language-reference/toc.yml index 8d2ed14b8379c..bf79e63651aa8 100644 --- a/docs/csharp/language-reference/toc.yml +++ b/docs/csharp/language-reference/toc.yml @@ -651,7 +651,7 @@ items: CS0105, CS0138, CS0430, CS0431, CS0432, CS0439, CS0440, CS0576, CS0687, CS1529, CS1537, CS1671, CS1679, CS1680, CS1681, CS1730, CS2034, CS7000, CS7007, CS7015, CS8019, CS8083, CS8085, CS8914, CS8915, CS8933, CS8954, CS8955, CS8956, CS9130, - CS9131, CS9132, CS9133, CS9162, CS9163 + CS9131, CS9132, CS9133, CS9162 - name: Using statements and declarations href: ./compiler-messages/using-statement-declaration-errors.md displayName: > From f46fb50bd0984dc0dde907c33f9d6d3a53c4877c Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Mon, 9 Feb 2026 11:02:09 -0500 Subject: [PATCH 05/12] fix warnings --- .../compatibility/windows-forms/10.0/menuitem-contextmenu.md | 2 +- .../compiler-messages/using-directive-errors.md | 5 ++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/docs/core/compatibility/windows-forms/10.0/menuitem-contextmenu.md b/docs/core/compatibility/windows-forms/10.0/menuitem-contextmenu.md index 7a48dc2858196..18d56c8cf7075 100644 --- a/docs/core/compatibility/windows-forms/10.0/menuitem-contextmenu.md +++ b/docs/core/compatibility/windows-forms/10.0/menuitem-contextmenu.md @@ -48,7 +48,7 @@ Use aliases to resolve conflicting namespaces. For example: using ContextMenu = System.Windows.Controls.ContextMenu; ``` -Refer to the [alias name conflicts documentation](../../../../csharp/language-reference/compiler-messages/using-directive-errors.md#alias-name-conflicts) for more details. +Refer to the [alias name conflicts documentation](../../../../csharp/language-reference/compiler-messages/using-directive-errors.md#alias-qualifier) for more details. ## Affected APIs diff --git a/docs/csharp/language-reference/compiler-messages/using-directive-errors.md b/docs/csharp/language-reference/compiler-messages/using-directive-errors.md index 69cf30b8faf69..695e5112d5ccf 100644 --- a/docs/csharp/language-reference/compiler-messages/using-directive-errors.md +++ b/docs/csharp/language-reference/compiler-messages/using-directive-errors.md @@ -154,7 +154,7 @@ The following errors relate to `using static` directives: - **CS9133**: *`static` modifier must precede `unsafe` modifier.* - **CS9162**: *Type is not valid for 'using static'. Only a class, struct, interface, enum, delegate, or namespace can be used.* -See the [using static directive](../keywords/using-directive.md#the-using-static-modifier) language reference for usage rules. +See the [using static directive](../keywords/using-directive.md#the-static-modifier) language reference for usage rules. Add the `static` modifier when importing a type's members directly, because omitting `static` tells the compiler you're importing a namespace rather than a type (**CS0138**). Remove the `static` modifier when importing a namespace, because `using static` can only be applied to types, not namespaces (**CS7007**). Ensure the target of a `using static` directive is a class, struct, interface, enum, or delegate, because other types aren't valid targets for static imports (**CS9162**). Place the `static` modifier before the `unsafe` modifier when combining both, because the language requires modifiers in a specific order (**CS9133**). @@ -165,7 +165,7 @@ The following errors relate to `global using` directives: - **CS8914**: *A global using directive cannot be used in a namespace declaration.* - **CS8915**: *A global using directive must precede all non-global using directives.* -See the [global using directive](../keywords/using-directive.md#global-modifier) language reference for usage rules. +See the [global using directive](../keywords/using-directive.md#the-global-modifier) language reference for usage rules. Move `global using` directives outside of any namespace declaration to file scope, because global usings apply project-wide and can't be scoped to a namespace (**CS8914**). Place all `global using` directives before any non-global `using` directives in the file, because the language requires global directives to precede local ones (**CS8915**). Note that a `static global using` directive can't reference a [file-local](../keywords/file.md) type. @@ -213,4 +213,3 @@ See the [using alias](../keywords/using-directive.md#the-using-alias) language r Choose a unique name for your alias that doesn't conflict with existing type or namespace names in scope, because the compiler can't distinguish between the alias and the existing definition (**CS0576**). Use each alias name only once within a namespace, because duplicate alias declarations create ambiguity (**CS1537**). Remove the `static` modifier when declaring an alias, because aliases and static imports are mutually exclusive - use either `using static` to import members or `using Alias =` to create an alias, but not both together (**CS8085**). Starting with C# 12, the following restrictions apply to using aliases: Don't use `ref`, `in`, or `out` modifiers in a using alias, because these parameter modifiers aren't valid in type alias contexts (**CS9130**). Use the `unsafe` modifier only with aliases that reference pointer types or with `using static` directives, because `unsafe` without an alias or static import isn't permitted (**CS9131**). Use a non-nullable reference type when creating an alias to a reference type, because nullable reference types can't be aliased directly (**CS9132**). - From abd3539c9894595ecccb2d60083b9c91ffb9ae56 Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Mon, 9 Feb 2026 11:29:19 -0500 Subject: [PATCH 06/12] Apply suggestions from code review Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- .../compatibility/windows-forms/10.0/menuitem-contextmenu.md | 2 +- .../compiler-messages/using-directive-errors.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/core/compatibility/windows-forms/10.0/menuitem-contextmenu.md b/docs/core/compatibility/windows-forms/10.0/menuitem-contextmenu.md index 18d56c8cf7075..de63f39374ca3 100644 --- a/docs/core/compatibility/windows-forms/10.0/menuitem-contextmenu.md +++ b/docs/core/compatibility/windows-forms/10.0/menuitem-contextmenu.md @@ -48,7 +48,7 @@ Use aliases to resolve conflicting namespaces. For example: using ContextMenu = System.Windows.Controls.ContextMenu; ``` -Refer to the [alias name conflicts documentation](../../../../csharp/language-reference/compiler-messages/using-directive-errors.md#alias-qualifier) for more details. +Refer to the [alias name conflicts documentation](../../../../csharp/language-reference/compiler-messages/using-directive-errors.md#using-alias-restrictions) for more details. ## Affected APIs diff --git a/docs/csharp/language-reference/compiler-messages/using-directive-errors.md b/docs/csharp/language-reference/compiler-messages/using-directive-errors.md index 695e5112d5ccf..8f2042eb073ca 100644 --- a/docs/csharp/language-reference/compiler-messages/using-directive-errors.md +++ b/docs/csharp/language-reference/compiler-messages/using-directive-errors.md @@ -103,7 +103,7 @@ That's be design. The text closely matches the text of the compiler error / warn - [**CS8914**](#global-using-directive): *Error: A global using directive cannot be used in a namespace declaration.* - [**CS8915**](#global-using-directive): *Error: A global using directive must precede all non-global using directives.* - [**CS8954**](#file-scoped-namespace): *Error: Source file can only contain one file-scoped namespace declaration.* -- [**CS8955**](#file-scoped-namespace): *Error: Source file can not contain both file-scoped and normal namespace declarations.* +- [**CS8955**](#file-scoped-namespace): *Error: Source file cannot contain both file-scoped and normal namespace declarations.* - [**CS8956**](#file-scoped-namespace): *Error: File-scoped namespace must precede all other members in a file.* - [**CS9130**](#using-alias-restrictions): *Error: Using alias cannot be a `ref` type.* - [**CS9131**](#using-alias-restrictions): *Error: Only a using alias can be `unsafe`.* From 7609d0b4142a64d74e46af3a854d009876f90994 Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Mon, 9 Feb 2026 16:26:27 -0500 Subject: [PATCH 07/12] Update docs/csharp/language-reference/compiler-messages/using-directive-errors.md Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> From 728fb2e056e74a1a0df7f01239f1e2ec17617c2a Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Tue, 10 Feb 2026 14:27:13 -0500 Subject: [PATCH 08/12] Add diagnostics for declaring namespaces --- .openpublishing.redirection.csharp.json | 36 ++ .../compiler-messages/cs0116.md | 104 ---- .../compiler-messages/cs0518.md | 56 -- .../using-directive-errors.md | 497 +++++++++++++++++- docs/csharp/language-reference/toc.yml | 23 +- docs/csharp/misc/cs0104.md | 47 -- docs/csharp/misc/cs0434.md | 62 --- docs/csharp/misc/cs0435.md | 50 -- docs/csharp/misc/cs0436.md | 80 --- docs/csharp/misc/cs0437.md | 57 -- docs/csharp/misc/cs0438.md | 53 -- docs/csharp/misc/cs1022.md | 25 - ...n-t-have-specifics-on-this-csharp-error.md | 2 - 13 files changed, 531 insertions(+), 561 deletions(-) delete mode 100644 docs/csharp/language-reference/compiler-messages/cs0116.md delete mode 100644 docs/csharp/language-reference/compiler-messages/cs0518.md delete mode 100644 docs/csharp/misc/cs0104.md delete mode 100644 docs/csharp/misc/cs0434.md delete mode 100644 docs/csharp/misc/cs0435.md delete mode 100644 docs/csharp/misc/cs0436.md delete mode 100644 docs/csharp/misc/cs0437.md delete mode 100644 docs/csharp/misc/cs0438.md delete mode 100644 docs/csharp/misc/cs1022.md diff --git a/.openpublishing.redirection.csharp.json b/.openpublishing.redirection.csharp.json index a366fffbe5454..27d77f08a945a 100644 --- a/.openpublishing.redirection.csharp.json +++ b/.openpublishing.redirection.csharp.json @@ -96,6 +96,34 @@ "source_path_from_root": "/docs/csharp/misc/cs0717.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/generic-type-parameters-errors" }, + { + "source_path_from_root": "/docs/csharp/misc/cs0104.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/using-directive-errors#cs0104" + }, + { + "source_path_from_root": "/docs/csharp/misc/cs0434.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/using-directive-errors#cs0434" + }, + { + "source_path_from_root": "/docs/csharp/misc/cs0435.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/using-directive-errors#cs0435" + }, + { + "source_path_from_root": "/docs/csharp/misc/cs0436.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/using-directive-errors#cs0436" + }, + { + "source_path_from_root": "/docs/csharp/misc/cs0437.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/using-directive-errors#cs0437" + }, + { + "source_path_from_root": "/docs/csharp/misc/cs0438.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/using-directive-errors#cs0438" + }, + { + "source_path_from_root": "/docs/csharp/misc/cs1022.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/using-directive-errors#cs1022" + }, { "source_path_from_root": "/docs/csharp/language-reference/compiler-messages/cs0304.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/generic-type-parameters-errors" @@ -443,6 +471,10 @@ "source_path_from_root": "/docs/csharp/language-reference/compiler-messages/cs0106.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/interface-implementation-errors" }, + { + "source_path_from_root": "/docs/csharp/language-reference/compiler-messages/cs0116.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/using-directive-errors#cs0116" + }, { "source_path_from_root": "/docs/csharp/misc/cs0277.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/interface-implementation-errors" @@ -475,6 +507,10 @@ "source_path_from_root": "/docs/csharp/language-reference/compiler-messages/cs0270.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/array-declaration-errors" }, + { + "source_path_from_root": "/docs/csharp/language-reference/compiler-messages/cs0518.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/using-directive-errors#cs0518" + }, { "source_path_from_root": "/docs/csharp/language-reference/compiler-messages/cs0545.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/property-declaration-errors" diff --git a/docs/csharp/language-reference/compiler-messages/cs0116.md b/docs/csharp/language-reference/compiler-messages/cs0116.md deleted file mode 100644 index c44dee3de13ee..0000000000000 --- a/docs/csharp/language-reference/compiler-messages/cs0116.md +++ /dev/null @@ -1,104 +0,0 @@ ---- -description: "Compiler Error CS0116" -title: "Compiler Error CS0116" -ms.date: 12/04/2018 -f1_keywords: - - "CS0116" -helpviewer_keywords: - - "CS0116" -ms.assetid: 4cb137b5-ec29-4c1a-adde-9f8424cb9496 ---- -# Compiler Error CS0116 - -A namespace cannot directly contain members such as fields or methods. - -A namespace can contain other namespaces, structs, and classes. For more information, see the [namespace keyword](../keywords/namespace.md) article. - -## Example - -The following sample will cause Visual Studio to flag parts of the code as being in violation of CS0116. Attempting to build this code will result in build failure: - -```csharp -// CS0116.cs -namespace x -{ - // A namespace can be placed within another namespace. - using System; - - // These variables trigger the CS0116 error as they are declared outside of a struct or class. - public int latitude; - public int longitude; - Coordinate coord; - - // Auto-properties also fall under the definition of this rule. - public string LocationName { get; set; } - - // This method as well: if it isn't in a class or a struct, it's violating CS0116. - public void DisplayLatitude() - { - Console.WriteLine($"Lat: {latitude}"); - } - - public struct Coordinate - { - } - - public class CoordinatePrinter - { - public void DisplayLongitude() - { - Console.WriteLine($"Longitude: {longitude}"); - } - - public void DisplayLocation() - { - Console.WriteLine($"Location: {LocationName}"); - } - } -} -``` - -Note that in C#, methods and variables must be declared and defined within a struct or class. For more information on program structure in C#, see the [General Structure of a C# Program](../../fundamentals/program-structure/index.md) article. To fix this error, rewrite your code such that all methods and fields are contained within either a struct or a class: - -```csharp -namespace x -{ - // A namespace can be placed within another namespace. - using System; - - // These variables are now placed within a struct, so CS0116 is no longer violated. - public struct Coordinate - { - public int Latitude; - public int Longitude; - } - - // The methods and fields are now placed within a class, and the compiler is satisfied. - public class CoordinatePrinter - { - Coordinate coord; - public string LocationName { get; set; } - - public void DisplayLatitude() - { - Console.WriteLine($"Lat: {coord.Latitude}"); - } - - public void DisplayLongitude() - { - Console.WriteLine($"Longitude: {coord.Longitude}"); - } - - public void DisplayLocation() - { - Console.WriteLine($"Location: {LocationName}"); - } - } -} -``` - -## See also - -- [General Structure of a C# Program](../../fundamentals/program-structure/index.md) -- [The C# type system](../../fundamentals/types/index.md) -- [Namespaces](../../fundamentals/types/namespaces.md) diff --git a/docs/csharp/language-reference/compiler-messages/cs0518.md b/docs/csharp/language-reference/compiler-messages/cs0518.md deleted file mode 100644 index b3ac64dc573cc..0000000000000 --- a/docs/csharp/language-reference/compiler-messages/cs0518.md +++ /dev/null @@ -1,56 +0,0 @@ ---- -description: "Compiler Error CS0518" -title: "Compiler Error CS0518" -ms.date: 07/20/2015 -f1_keywords: - - "CS0518" -helpviewer_keywords: - - "CS0518" -ms.assetid: b0b61cbb-c9a7-48c9-9e60-7cdd5ecb3e6c ---- -# Compiler Error CS0518 - -Predefined type 'type' is not defined or imported - -> [!NOTE] -> The resolution for this error depends on whether you're using a modern SDK-style project (`.csproj` files that start with ``) or legacy project formats. SDK-style projects manage runtime references automatically through the `` property. - -The main cause for this problem is that the project cannot access the predefined types from the .NET runtime library. In modern SDK-style projects, this is typically due to an incorrect or missing `` specification. In legacy projects, this issue is caused by not importing mscorlib.dll, which defines the entire namespace. This can be caused by one of the following: - -[!INCLUDE[csharp-build-only-diagnostic-note](~/includes/csharp-build-only-diagnostic-note.md)] - -- The [**NoStandardLib**](../compiler-options/advanced.md#nostandardlib) option from the command line compiler has been specified. The **NoStandardLib** option prevents the import of mscorlib.dll. Use this option if you want to define or create a user-specific System namespace. - -- An incorrect mscorlib.dll is referenced. - -- A corrupt Visual Studio .NET or .NET Framework common language runtime installation exists. - -- Residual components from an earlier installation that are incompatible with the latest installation remain. - - To resolve this problem, take one of the following actions: - -- Do not specify the /nostdlib option from the command line compiler. - -- For modern SDK-style projects, ensure the project targets the correct .NET runtime. In your `.csproj` file, verify the `` property specifies the intended runtime: - - ```xml - - net8.0 - - ``` - - For multi-targeting projects, use `` (plural): - - ```xml - - net8.0;net48 - - ``` - -- For legacy project formats, make sure that the project refers to the correct mscorlib.dll. - -- Reinstall the .NET Framework common language runtime (if the previous solutions do not solve the problem). - -- Reload the project in Visual Studio. - -- Close Visual Studio, delete the `obj` and `bin` folders from your project directory, then reopen Visual Studio and rebuild the project. diff --git a/docs/csharp/language-reference/compiler-messages/using-directive-errors.md b/docs/csharp/language-reference/compiler-messages/using-directive-errors.md index 8f2042eb073ca..0ae3091ea756d 100644 --- a/docs/csharp/language-reference/compiler-messages/using-directive-errors.md +++ b/docs/csharp/language-reference/compiler-messages/using-directive-errors.md @@ -1,17 +1,26 @@ --- title: "Resolve compiler errors and warnings related to using directives and using alias directives" description: "These errors and warnings indicate problems with using directives and using directive aliases. This information helps diagnose and fix those issues." -ms.date: 02/06/2026 +ms.date: 02/10/2026 f1_keywords: + - "CS0104" - "CS0105" + - "CS0116" - "CS0138" - "CS0430" - "CS0431" - "CS0432" + - "CS0434" + - "CS0435" + - "CS0436" + - "CS0437" + - "CS0438" - "CS0439" - "CS0440" + - "CS0518" - "CS0576" - "CS0687" + - "CS1022" - "CS1529" - "CS1537" - "CS1671" @@ -23,7 +32,9 @@ f1_keywords: - "CS7000" - "CS7007" - "CS7015" + - "CS7021" - "CS8019" + - "CS8020" - "CS8083" - "CS8085" - "CS8914" @@ -38,15 +49,24 @@ f1_keywords: - "CS9133" - "CS9162" helpviewer_keywords: + - "CS0104" - "CS0105" + - "CS0116" - "CS0138" - "CS0430" - "CS0431" - "CS0432" + - "CS0434" + - "CS0435" + - "CS0436" + - "CS0437" + - "CS0438" - "CS0439" - "CS0440" + - "CS0518" - "CS0576" - "CS0687" + - "CS1022" - "CS1529" - "CS1537" - "CS1671" @@ -58,7 +78,9 @@ helpviewer_keywords: - "CS7000" - "CS7007" - "CS7015" + - "CS7021" - "CS8019" + - "CS8020" - "CS8083" - "CS8085" - "CS8914" @@ -73,18 +95,23 @@ helpviewer_keywords: - "CS9133" - "CS9162" --- -# Resolve warnings related using namespaces +# Resolve warnings related to using and declaring namespaces This article covers the following compiler errors: +- [**CS0104**](#cs0104): *Error: 'reference' is an ambiguous reference between 'identifier' and 'identifier'.* +- [**CS0116**](#cs0116): *Error: A namespace cannot directly contain members such as fields, methods or statements.* - [**CS0138**](#using-static-directive): *Error: A using namespace directive can only be applied to namespaces; 'type' is a type not a namespace.* - [**CS0430**](#using-directive): *Error: The extern alias 'alias' was not specified in a /reference option.* - [**CS0431**](#alias-qualifier): *Error: Cannot use alias 'identifier' with `::` since the alias references a type. Use `.` instead*. - [**CS0432**](#alias-qualifier): *Error: Alias 'identifier' not found.* +- [**CS0434**](#cs0434): *Error: The namespace NamespaceName1 in NamespaceName2 conflicts with the type TypeName1 in NamespaceName3.* +- [**CS0438**](#cs0438): *Error: The type 'type' in 'module_1' conflicts with the namespace 'namespace' in 'module_2'.* - [**CS0439**](#using-directive): *Error: An extern alias declaration must precede all other elements defined in the namespace.* +- [**CS0518**](#cs0518): *Error: Predefined type 'type' is not defined or imported.* - [**CS0576**](#using-alias-restrictions): *Error: Namespace 'namespace' contains a definition conflicting with alias 'identifier'.* - [**CS0687**](#alias-qualifier): *Error: The namespace alias qualifier `::` always resolves to a type or namespace so is illegal here. Consider using `.` instead.* - [**CS1529**](#using-directive): *Error: A using clause must precede all other elements defined in the namespace except extern alias declarations.* @@ -103,8 +130,10 @@ That's be design. The text closely matches the text of the compiler error / warn - [**CS8914**](#global-using-directive): *Error: A global using directive cannot be used in a namespace declaration.* - [**CS8915**](#global-using-directive): *Error: A global using directive must precede all non-global using directives.* - [**CS8954**](#file-scoped-namespace): *Error: Source file can only contain one file-scoped namespace declaration.* -- [**CS8955**](#file-scoped-namespace): *Error: Source file cannot contain both file-scoped and normal namespace declarations.* +- [**CS8955**](#file-scoped-namespace): *Error: Source file can not contain both file-scoped and normal namespace declarations.* - [**CS8956**](#file-scoped-namespace): *Error: File-scoped namespace must precede all other members in a file.* +- [**CS1022**](#cs1022): *Error: Type or namespace definition, or end-of-file expected.* +- [**CS7021**](#cs7021): *Error: Cannot declare namespace in script code.* - [**CS9130**](#using-alias-restrictions): *Error: Using alias cannot be a `ref` type.* - [**CS9131**](#using-alias-restrictions): *Error: Only a using alias can be `unsafe`.* - [**CS9132**](#using-alias-restrictions): *Error: Using alias cannot be a nullable reference type.* @@ -115,7 +144,11 @@ And the following compiler warnings: - [**CS0105**](#using-directive): *Warning: The using directive for 'namespace' appeared previously in this namespace.* - [**CS0440**](#alias-qualifier): *Warning: Defining an alias named `global` is ill-advised since `global::` always references the global namespace and not an alias.* +- [**CS0435**](#cs0435): *Warning: The namespace 'namespace' in 'assembly' conflicts with the imported type 'type' in 'assembly'. Using the namespace defined in 'assembly'.* +- [**CS0436**](#cs0436): *Warning: The type 'type' in 'assembly' conflicts with the imported type 'type2' in 'assembly'. Using the type defined in 'assembly'.* +- [**CS0437**](#cs0437): *Warning: The type 'type' in 'assembly2' conflicts with the imported namespace 'namespace' in 'assembly1'. Using the type defined in 'assembly'.* - [**CS8019**](#using-directive): *Info: Unnecessary using directive.* +- [**CS8020**](#using-directive): *Info: Unused extern alias.* - [**CS8933**](#using-directive): *Info: The using directive appeared previously as global using.* These errors and warnings indicate your `using` directive isn't formed correctly. The following sections cover these errors and how to correct them. @@ -135,6 +168,7 @@ The following errors relate to `using` directives: - **CS2034**: *A /reference option that declares an extern alias can only have one filename. To specify multiple aliases or filenames, use multiple /reference options.* - **CS7015**: *'extern alias' is not valid in this context.* - **CS8019**: *Unnecessary using directive.* +- **CS8020**: *Unused extern alias.* - **CS8933**: *The using directive appeared previously as global using.* See the [using directive](../keywords/using-directive.md) and [extern alias](../keywords/extern-alias.md) language reference for usage rules. @@ -143,7 +177,7 @@ Move all `using` directives to the top of the file, or to the top of the namespa Ensure that every `extern alias` declaration in your source code has a corresponding alias defined in your project's [reference options](../compiler-options/inputs.md#references), because the compiler can't resolve an alias that wasn't specified (**CS0430**). Use a separate `/reference` option for each extern alias rather than combining multiple aliases in a single option, because the compiler requires one alias per reference option (**CS2034**). Ensure the alias in your `/reference` option is a valid C# identifier, because the alias must follow identifier naming rules (**CS1679**). Include a filename after the `=` sign in your alias reference option, because the compiler needs to know which assembly the alias refers to (**CS1680**). Don't attempt to redefine the `global` extern alias, because `global` is a predefined alias that refers to all unaliased references (**CS1681**). -Remove duplicate `using` directives, because the compiler warns when the same namespace is imported multiple times (**CS0105**, **CS8019**, **CS8933**). +Remove duplicate `using` directives, because the compiler warns when the same namespace is imported multiple times (**CS0105**, **CS8019**, **CS8933**). Remove unused `extern alias` declarations, because the compiler issues a diagnostic when an extern alias is declared but never referenced in your code (**CS8020**). ## Using static directive @@ -213,3 +247,456 @@ See the [using alias](../keywords/using-directive.md#the-using-alias) language r Choose a unique name for your alias that doesn't conflict with existing type or namespace names in scope, because the compiler can't distinguish between the alias and the existing definition (**CS0576**). Use each alias name only once within a namespace, because duplicate alias declarations create ambiguity (**CS1537**). Remove the `static` modifier when declaring an alias, because aliases and static imports are mutually exclusive - use either `using static` to import members or `using Alias =` to create an alias, but not both together (**CS8085**). Starting with C# 12, the following restrictions apply to using aliases: Don't use `ref`, `in`, or `out` modifiers in a using alias, because these parameter modifiers aren't valid in type alias contexts (**CS9130**). Use the `unsafe` modifier only with aliases that reference pointer types or with `using static` directives, because `unsafe` without an alias or static import isn't permitted (**CS9131**). Use a non-nullable reference type when creating an alias to a reference type, because nullable reference types can't be aliased directly (**CS9132**). + +## CS0116 + +A namespace cannot directly contain members such as fields or methods. + +A namespace can contain other namespaces, structs, and classes. For more information, see the [namespace keyword](../keywords/namespace.md) article. + +### Example + +The following sample will cause Visual Studio to flag parts of the code as being in violation of CS0116. Attempting to build this code will result in build failure: + +```csharp +// CS0116.cs +namespace x +{ + // A namespace can be placed within another namespace. + using System; + + // These variables trigger the CS0116 error as they are declared outside of a struct or class. + public int latitude; + public int longitude; + Coordinate coord; + + // Auto-properties also fall under the definition of this rule. + public string LocationName { get; set; } + + // This method as well: if it isn't in a class or a struct, it's violating CS0116. + public void DisplayLatitude() + { + Console.WriteLine($"Lat: {latitude}"); + } + + public struct Coordinate + { + } + + public class CoordinatePrinter + { + public void DisplayLongitude() + { + Console.WriteLine($"Longitude: {longitude}"); + } + + public void DisplayLocation() + { + Console.WriteLine($"Location: {LocationName}"); + } + } +} +``` + +Note that in C#, methods and variables must be declared and defined within a struct or class. For more information on program structure in C#, see the [General Structure of a C# Program](../../fundamentals/program-structure/index.md) article. To fix this error, rewrite your code such that all methods and fields are contained within either a struct or a class: + +```csharp +namespace x +{ + // A namespace can be placed within another namespace. + using System; + + // These variables are now placed within a struct, so CS0116 is no longer violated. + public struct Coordinate + { + public int Latitude; + public int Longitude; + } + + // The methods and fields are now placed within a class, and the compiler is satisfied. + public class CoordinatePrinter + { + Coordinate coord; + public string LocationName { get; set; } + + public void DisplayLatitude() + { + Console.WriteLine($"Lat: {coord.Latitude}"); + } + + public void DisplayLongitude() + { + Console.WriteLine($"Longitude: {coord.Longitude}"); + } + + public void DisplayLocation() + { + Console.WriteLine($"Location: {LocationName}"); + } + } +} +``` + +## CS0518 + +Predefined type 'type' is not defined or imported. + +> [!NOTE] +> The resolution for this error depends on whether you're using a modern SDK-style project (`.csproj` files that start with ``) or legacy project formats. SDK-style projects manage runtime references automatically through the `` property. + +The main cause for this problem is that the project cannot access the predefined types from the .NET runtime library. In modern SDK-style projects, this is typically due to an incorrect or missing `` specification. In legacy projects, this issue is caused by not importing mscorlib.dll, which defines the entire namespace. This can be caused by one of the following: + +[!INCLUDE[csharp-build-only-diagnostic-note](~/includes/csharp-build-only-diagnostic-note.md)] + +- The [**NoStandardLib**](../compiler-options/advanced.md#nostandardlib) option from the command line compiler has been specified. The **NoStandardLib** option prevents the import of mscorlib.dll. Use this option if you want to define or create a user-specific System namespace. + +- An incorrect mscorlib.dll is referenced. + +- A corrupt Visual Studio .NET or .NET Framework common language runtime installation exists. + +- Residual components from an earlier installation that are incompatible with the latest installation remain. + +To resolve this problem, take one of the following actions: + +- Do not specify the /nostdlib option from the command line compiler. + +- For modern SDK-style projects, ensure the project targets the correct .NET runtime. In your `.csproj` file, verify the `` property specifies the intended runtime: + + ```xml + + net8.0 + + ``` + + For multi-targeting projects, use `` (plural): + + ```xml + + net8.0;net48 + + ``` + +- For legacy project formats, make sure that the project refers to the correct mscorlib.dll. + +- Reinstall the .NET Framework common language runtime (if the previous solutions do not solve the problem). + +- Reload the project in Visual Studio. + +- Close Visual Studio, delete the `obj` and `bin` folders from your project directory, then reopen Visual Studio and rebuild the project. + +## CS0104 + +'reference' is an ambiguous reference between 'identifier' and 'identifier'. + +Your program contains [using](../keywords/using-directive.md) directives for two namespaces and your code references a name that appears in both namespaces. + +The following sample generates CS0104: + +```csharp +// CS0104.cs +using x; +using y; + +namespace x +{ + public class Test + { + } +} + +namespace y +{ + public class Test + { + } +} + +public class a +{ + public static void Main() + { + Test test = new Test(); // CS0104, is Test in x or y namespace? + // try the following line instead + // y.Test test = new y.Test(); + } +} +``` + +## CS0434 + +The namespace NamespaceName1 in NamespaceName2 conflicts with the type TypeName1 in NamespaceName3. + +This error occurs when an imported type and an imported nested namespace have the same fully qualified name. When that name is referenced, the compiler is unable to distinguish between the two. If you can change the imported source code, you can resolve the error by changing the name of either the type or the namespace so that both are unique within the assembly. + +The following code generates error CS0434. + +### Example 1 + +This code creates the first copy of the type with the identical fully qualified name. + +```csharp +// CS0434_1.cs +// compile with: /t:library +namespace TypeBindConflicts +{ + namespace NsImpAggPubImp + { + public class X { } + } +} +``` + +### Example 2 + +This code creates the second copy of the type with the identical fully qualified name. + +```csharp +// CS0434_2.cs +// compile with: /t:library +namespace TypeBindConflicts { + // Conflicts with another import (import2.cs). + public class NsImpAggPubImp { } + // Try this instead: + // public class UniqueClassName { } +} +``` + +### Example 3 + +This code references the type with the identical fully qualified name. + +```csharp +// CS0434.cs +// compile with: /r:cs0434_1.dll /r:cs0434_2.dll +using TypeBindConflicts; +public class Test +{ + public TypeBindConflicts.NsImpAggPubImp.X n2 = null; // CS0434 +} +``` + +## CS0435 + +The namespace 'namespace' in 'assembly' conflicts with the imported type 'type' in 'assembly'. Using the namespace defined in 'assembly'. + +This warning is issued when a namespace in a source file (file_2) conflicts with an imported type in file_1. The compiler uses the one in the source file. + +The following example generates CS0435: + +Compile this file first: + +```csharp +// CS0435_1.cs +// compile with: /t:library +public class Util +{ + public class A { } +} +``` + +Then, compile this file: + +```csharp +// CS0435_2.cs +// compile with: /r:CS0435_1.dll + +using System; + +namespace Util +{ + public class A { } +} + +public class Test +{ + public static void Main() + { + Console.WriteLine(typeof(Util.A)); // CS0435 + } +} +``` + +## CS0436 + +The type 'type' in 'assembly' conflicts with the imported type 'type2' in 'assembly'. Using the type defined in 'assembly'. + +This warning occurs when a type defined in your source code has the same fully qualified name (namespace and type name) as a type imported from a referenced assembly. When this name conflict occurs, the compiler uses the locally defined type from your source file and ignores the imported type. + +### What constitutes a conflict + +A conflict occurs when two types have identical fully qualified names, meaning: + +- They have the same namespace +- They have the same type name +- They're both accessible in the current compilation context + +The conflict is determined solely by the type's fully qualified name, not by its implementation details. Two types with the same name but different implementations (such as different methods, properties, or field values) still conflict. The compiler can't use both types simultaneously because they have the same identity. + +### Example + +The following example demonstrates CS0436. In this scenario, a type `A` is defined in an external library and also locally in the source file. Even though the two types have different implementations (they print different strings), they conflict because they share the same fully qualified name. + +First, create a library that defines type `A`: + +```csharp +// CS0436_a.cs +// compile with: /target:library +public class A { + public void Test() { + System.Console.WriteLine("CS0436_a"); + } +} +``` + +Then, compile the following code that defines another type `A` and references the library. The compiler issues CS0436 because both types have the fully qualified name `A` (in the global namespace): + +```csharp +// CS0436_b.cs +// compile with: /reference:CS0436_a.dll +// CS0436 expected +public class A { + public void Test() { + System.Console.WriteLine("CS0436_b"); + } +} + +public class Test +{ + public static void Main() + { + A x = new A(); + x.Test(); + } +} +``` + +When you compile and run this code, the compiler uses the locally defined `A` (from CS0436_b.cs) and issues a warning. The output is: + +```console +CS0436_b +``` + +Note that the conflict exists even though the two `A` types have different implementations. The difference in the string literal (`"CS0436_a"` versus `"CS0436_b"`) doesn't prevent the conflict. What matters is that both types have the same fully qualified name `A`. + +### How to resolve this warning + +To resolve this warning, you can: + +1. Rename one of the conflicting types. +1. Use a different namespace for one of the types. +1. Remove the reference to the assembly containing the conflicting type if it's not needed. +1. Use an extern alias to disambiguate between the two types if you need to use both (see [CS0433](../language-reference/compiler-messages/cs0433.md) for examples of using extern aliases). + +## CS0437 + +The type 'type' in 'assembly2' conflicts with the imported namespace 'namespace' in 'assembly1'. Using the type defined in 'assembly'. + +This warning is issued when a type in a source file, file_2, conflicts with an imported namespace in file_1. The compiler uses the type in the source file. + +### Example 1 + +```csharp +// CS0437_a.cs +// compile with: /target:library +namespace Util +{ + public class A { + public void Test() { + System.Console.WriteLine("CS0437_a.cs"); + } + } +} +``` + +### Example 2 + +The following sample generates CS0437. + +```csharp +// CS0437_b.cs +// compile with: /reference:CS0437_a.dll /W:2 +// CS0437 expected +class Util +{ + public class A { + public void Test() { + System.Console.WriteLine("CS0437_b.cs"); + } + } +} + +public class Test +{ + public static void Main() + { + Util.A x = new Util.A(); + x.Test(); + } +} +``` + +## CS0438 + +The type 'type' in 'module_1' conflicts with the namespace 'namespace' in 'module_2'. + +This error occurs when a type in a source file conflicts with a namespace in another source file. This typically happens when one or both come from an added module. To resolve, rename the type or the namespace that caused the conflict. + +The following example generates CS0438: + +Compile this file first: + +```csharp +// CS0438_1.cs +// compile with: /target:module +public class Util +{ + public class A { } +} +``` + +Then compile this file: + +```csharp +// CS0438_2.cs +// compile with: /target:module +namespace Util +{ + public class A { } +} +``` + +And then compile this file: + +```csharp +// CS0438_3.cs +// compile with: /addmodule:CS0438_1.netmodule /addmodule:CS0438_2.netmodule +using System; +public class Test +{ + public static void Main() { + Console.WriteLine(typeof(Util.A)); // CS0438 + } +} +``` + +## CS1022 + +Type or namespace definition, or end-of-file expected. + +A source-code file does not have a matching set of braces. + +The following sample generates CS1022: + +```csharp +// CS1022.cs +namespace x +{ +} +} // CS1022 +``` + +## CS7021 + +You can't declare a namespace in script code. + +C# script files (`.csx`) don't support namespace declarations. All code in a script file is evaluated in a single execution context. If you need to organize code into namespaces, move it into a regular C# source file (`.cs`). diff --git a/docs/csharp/language-reference/toc.yml b/docs/csharp/language-reference/toc.yml index bf79e63651aa8..6afa637dc13dd 100644 --- a/docs/csharp/language-reference/toc.yml +++ b/docs/csharp/language-reference/toc.yml @@ -648,9 +648,10 @@ items: - name: Using directive and aliases href: ./compiler-messages/using-directive-errors.md displayName: > - CS0105, CS0138, CS0430, CS0431, CS0432, CS0439, CS0440, CS0576, CS0687, CS1529, + CS0104, CS0105, CS0116, CS0138, CS0430, CS0431, CS0432, CS0434, CS0435, CS0436, CS0437, + CS0438, CS0439, CS0440, CS0518, CS0576, CS0687, CS1022, CS1529, CS1537, CS1671, CS1679, CS1680, CS1681, CS1730, CS2034, CS7000, CS7007, CS7015, - CS8019, CS8083, CS8085, CS8914, CS8915, CS8933, CS8954, CS8955, CS8956, CS9130, + CS7021, CS8019, CS8020, CS8083, CS8085, CS8914, CS8915, CS8933, CS8954, CS8955, CS8956, CS9130, CS9131, CS9132, CS9133, CS9162 - name: Using statements and declarations href: ./compiler-messages/using-statement-declaration-errors.md @@ -815,8 +816,6 @@ items: href: ../misc/cs0102.md - name: CS0103 href: ./compiler-messages/cs0103.md - - name: CS0104 - href: ../misc/cs0104.md - name: CS0107 href: ../misc/cs0107.md - name: CS0110 @@ -827,8 +826,6 @@ items: href: ../misc/cs0113.md - name: CS0115 href: ./compiler-messages/cs0115.md - - name: CS0116 - href: ./compiler-messages/cs0116.md - name: CS0117 href: ../misc/cs0117.md - name: CS0118 @@ -1013,10 +1010,6 @@ items: href: ../misc/cs0428.md - name: CS0433 href: ./compiler-messages/cs0433.md - - name: CS0434 - href: ../misc/cs0434.md - - name: CS0438 - href: ../misc/cs0438.md - name: CS0441 href: ../misc/cs0441.md - name: CS0442 @@ -1063,8 +1056,6 @@ items: href: ../misc/cs0509.md - name: CS0513 href: ../misc/cs0513.md - - name: CS0518 - href: ./compiler-messages/cs0518.md - name: CS0520 href: ../misc/cs0520.md - name: CS0523 @@ -1317,8 +1308,6 @@ items: href: ../misc/cs1017.md - name: CS1021 href: ../misc/cs1021.md - - name: CS1022 - href: ../misc/cs1022.md - name: CS1023 href: ../misc/cs1023.md - name: CS1026 @@ -1955,12 +1944,6 @@ items: href: ../misc/cs0279.md - name: CS0280 href: ../misc/cs0280.md - - name: CS0435 - href: ../misc/cs0435.md - - name: CS0436 - href: ../misc/cs0436.md - - name: CS0437 - href: ../misc/cs0437.md - name: CS0444 href: ../misc/cs0444.md - name: CS0458 diff --git a/docs/csharp/misc/cs0104.md b/docs/csharp/misc/cs0104.md deleted file mode 100644 index a22b9612d34ed..0000000000000 --- a/docs/csharp/misc/cs0104.md +++ /dev/null @@ -1,47 +0,0 @@ ---- -description: "Compiler Error CS0104" -title: "Compiler Error CS0104" -ms.date: 07/20/2015 -f1_keywords: - - "CS0104" -helpviewer_keywords: - - "CS0104" -ms.assetid: 1a7e9ae8-308b-441b-ba85-fac974222875 ---- -# Compiler Error CS0104 - -'reference' is an ambiguous reference between 'identifier' and 'identifier' - - Your program contains [using](../language-reference/keywords/using-directive.md) directives for two namespaces and your code references a name that appears in both namespaces. - - The following sample generates CS0104: - -```csharp -// CS0104.cs -using x; -using y; - -namespace x -{ - public class Test - { - } -} - -namespace y -{ - public class Test - { - } -} - -public class a -{ - public static void Main() - { - Test test = new Test(); // CS0104, is Test in x or y namespace? - // try the following line instead - // y.Test test = new y.Test(); - } -} -``` diff --git a/docs/csharp/misc/cs0434.md b/docs/csharp/misc/cs0434.md deleted file mode 100644 index 6b6dce9c819f1..0000000000000 --- a/docs/csharp/misc/cs0434.md +++ /dev/null @@ -1,62 +0,0 @@ ---- -description: "Compiler Error CS0434" -title: "Compiler Error CS0434" -ms.date: 07/20/2015 -f1_keywords: - - "CS0434" -helpviewer_keywords: - - "CS0434" -ms.assetid: 8f8871fc-a4bb-4a9e-ba19-999f4943001e ---- -# Compiler Error CS0434 - -The namespace NamespaceName1 in NamespaceName2 conflicts with the type TypeName1 in NamespaceName3 - - This error occurs when an imported type and an imported nested namespace have the same fully qualified name. When that name is referenced, the compiler is unable to distinguish between the two. If you can change the imported source code, you can resolve the error by changing the name of either the type or the namespace so that both are unique within the assembly. - - The following code generates error CS0434. - -## Example 1 - - This code creates the first copy of the type with the identical fully qualified name. - -```csharp -// CS0434_1.cs -// compile with: /t:library -namespace TypeBindConflicts -{ - namespace NsImpAggPubImp - { - public class X { } - } -} -``` - -## Example 2 - - This code creates the second copy of the type with the identical fully qualified name. - -```csharp -// CS0434_2.cs -// compile with: /t:library -namespace TypeBindConflicts { - // Conflicts with another import (import2.cs). - public class NsImpAggPubImp { } - // Try this instead: - // public class UniqueClassName { } -} -``` - -## Example 3 - - This code references the type with the identical fully qualified name. - -```csharp -// CS0434.cs -// compile with: /r:cs0434_1.dll /r:cs0434_2.dll -using TypeBindConflicts; -public class Test -{ - public TypeBindConflicts.NsImpAggPubImp.X n2 = null; // CS0434 -} -``` diff --git a/docs/csharp/misc/cs0435.md b/docs/csharp/misc/cs0435.md deleted file mode 100644 index f6771d3829816..0000000000000 --- a/docs/csharp/misc/cs0435.md +++ /dev/null @@ -1,50 +0,0 @@ ---- -description: "Compiler Warning (level 2) CS0435" -title: "Compiler Warning (level 2) CS0435" -ms.date: 07/20/2015 -f1_keywords: - - "CS0435" -helpviewer_keywords: - - "CS0435" -ms.assetid: e70cd8c1-d399-4af8-8b1e-69a1de389aad ---- -# Compiler Warning (level 2) CS0435 - -The namespace 'namespace' in 'assembly' conflicts with the imported type 'type' in 'assembly'. Using the namespace defined in 'assembly'. - - This warning is issued when a namespace in a source file (file_2) conflicts with an imported type in file_1. The compiler uses the one in the source file. - - The following example generates CS0435: - - Compile this file first: - -```csharp -// CS0435_1.cs -// compile with: /t:library -public class Util -{ - public class A { } -} -``` - - Then, compile this file: - -```csharp -// CS0435_2.cs -// compile with: /r:CS0435_1.dll - -using System; - -namespace Util -{ - public class A { } -} - -public class Test -{ - public static void Main() - { - Console.WriteLine(typeof(Util.A)); // CS0435 - } -} -``` diff --git a/docs/csharp/misc/cs0436.md b/docs/csharp/misc/cs0436.md deleted file mode 100644 index 7f08166c5196b..0000000000000 --- a/docs/csharp/misc/cs0436.md +++ /dev/null @@ -1,80 +0,0 @@ ---- -description: "Compiler Warning (level 2) CS0436" -title: "Compiler Warning (level 2) CS0436" -ms.date: 10/01/2025 -f1_keywords: - - "CS0436" -helpviewer_keywords: - - "CS0436" -ms.assetid: c4135d9d-3511-4bbc-9540-48c2091f869c ---- -# Compiler Warning (level 2) CS0436 - -The type 'type' in 'assembly' conflicts with the imported type 'type2' in 'assembly'. Using the type defined in 'assembly'. - -This warning occurs when a type defined in your source code has the same fully qualified name (namespace and type name) as a type imported from a referenced assembly. When this name conflict occurs, the compiler uses the locally defined type from your source file and ignores the imported type. - -## What constitutes a conflict - -A conflict occurs when two types have identical fully qualified names, meaning: - -- They have the same namespace -- They have the same type name -- They're both accessible in the current compilation context - -The conflict is determined solely by the type's fully qualified name, not by its implementation details. Two types with the same name but different implementations (such as different methods, properties, or field values) still conflict. The compiler can't use both types simultaneously because they have the same identity. - -## Example - -The following example demonstrates CS0436. In this scenario, a type `A` is defined in an external library and also locally in the source file. Even though the two types have different implementations (they print different strings), they conflict because they share the same fully qualified name. - -First, create a library that defines type `A`: - -```csharp -// CS0436_a.cs -// compile with: /target:library -public class A { - public void Test() { - System.Console.WriteLine("CS0436_a"); - } -} -``` - -Then, compile the following code that defines another type `A` and references the library. The compiler issues CS0436 because both types have the fully qualified name `A` (in the global namespace): - -```csharp -// CS0436_b.cs -// compile with: /reference:CS0436_a.dll -// CS0436 expected -public class A { - public void Test() { - System.Console.WriteLine("CS0436_b"); - } -} - -public class Test -{ - public static void Main() - { - A x = new A(); - x.Test(); - } -} -``` - -When you compile and run this code, the compiler uses the locally defined `A` (from CS0436_b.cs) and issues a warning. The output is: - -```console -CS0436_b -``` - -Note that the conflict exists even though the two `A` types have different implementations. The difference in the string literal (`"CS0436_a"` versus `"CS0436_b"`) doesn't prevent the conflict. What matters is that both types have the same fully qualified name `A`. - -## How to resolve this warning - -To resolve this warning, you can: - -1. Rename one of the conflicting types. -1. Use a different namespace for one of the types. -1. Remove the reference to the assembly containing the conflicting type if it's not needed. -1. Use an extern alias to disambiguate between the two types if you need to use both (see [CS0433](../language-reference/compiler-messages/cs0433.md) for examples of using extern aliases). diff --git a/docs/csharp/misc/cs0437.md b/docs/csharp/misc/cs0437.md deleted file mode 100644 index 362fb6adfe618..0000000000000 --- a/docs/csharp/misc/cs0437.md +++ /dev/null @@ -1,57 +0,0 @@ ---- -description: "Compiler Warning (level 2) CS0437" -title: "Compiler Warning (level 2) CS0437" -ms.date: 07/20/2015 -f1_keywords: - - "CS0437" -helpviewer_keywords: - - "CS0437" -ms.assetid: cba5c9b6-a0bc-4f19-b1f0-c1f66436ee91 ---- -# Compiler Warning (level 2) CS0437 - -The type 'type' in 'assembly2' conflicts with the imported namespace 'namespace' in 'fassembly1'. Using the type defined in 'assembly'. - - This warning is issued when a type in a source file, file_2, conflicts with an imported namespace in file_1. The compiler uses the type in the source file. - -## Example 1 - -```csharp -// CS0437_a.cs -// compile with: /target:library -namespace Util -{ - public class A { - public void Test() { - System.Console.WriteLine("CS0437_a.cs"); - } - } -} -``` - -## Example 2 - - The following sample generates CS0437. - -```csharp -// CS0437_b.cs -// compile with: /reference:CS0437_a.dll /W:2 -// CS0437 expected -class Util -{ - public class A { - public void Test() { - System.Console.WriteLine("CS0437_b.cs"); - } - } -} - -public class Test -{ - public static void Main() - { - Util.A x = new Util.A(); - x.Test(); - } -} -``` diff --git a/docs/csharp/misc/cs0438.md b/docs/csharp/misc/cs0438.md deleted file mode 100644 index de4b064b1149f..0000000000000 --- a/docs/csharp/misc/cs0438.md +++ /dev/null @@ -1,53 +0,0 @@ ---- -description: "Compiler Error CS0438" -title: "Compiler Error CS0438" -ms.date: 07/20/2015 -f1_keywords: - - "CS0438" -helpviewer_keywords: - - "CS0438" -ms.assetid: 92c91ecb-8d6a-4850-84eb-c095c3c957f1 ---- -# Compiler Error CS0438 - -The type 'type' in 'module_1' conflicts with the namespace 'namespace' in 'module_2'. - - This error occurs when a type in a source file conflicts with a namespace in another source file. This typically happens when one or both come from an added module. To resolve, rename the type or the namespace that caused the conflict. - - The following example generates CS0438: - - Compile this file first: - -```csharp -// CS0438_1.cs -// compile with: /target:module -public class Util -{ - public class A { } -} -``` - - Then compile this file: - -```csharp -// CS0438_2.cs -// compile with: /target:module -namespace Util -{ - public class A { } -} -``` - - And then compile this file: - -```csharp -// CS0438_3.cs -// compile with: /addmodule:CS0438_1.netmodule /addmodule:CS0438_2.netmodule -using System; -public class Test -{ - public static void Main() { - Console.WriteLine(typeof(Util.A)); // CS0438 - } -} -``` diff --git a/docs/csharp/misc/cs1022.md b/docs/csharp/misc/cs1022.md deleted file mode 100644 index b6756b962f39d..0000000000000 --- a/docs/csharp/misc/cs1022.md +++ /dev/null @@ -1,25 +0,0 @@ ---- -description: "Compiler Error CS1022" -title: "Compiler Error CS1022" -ms.date: 07/20/2015 -f1_keywords: - - "CS1022" -helpviewer_keywords: - - "CS1022" -ms.assetid: 76b9f32b-2ebf-471d-a635-852daf8877d7 ---- -# Compiler Error CS1022 - -Type or namespace definition, or end-of-file expected - - A source-code file does not have a matching set of braces. - - The following sample generates CS1022: - -```csharp -// CS1022.cs -namespace x -{ -} -} // CS1022 -``` 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 1d123e78f467f..3ac35932c4cf4 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 @@ -85,7 +85,6 @@ f1_keywords: - "CS7018" - "CS7019" - "CS7020" - - "CS7021" - "CS7022" # build only diagnostic - "CS7024" - "CS7025" @@ -158,7 +157,6 @@ f1_keywords: - "CS8016" - "CS8017" - "CS8018" - - "CS8020" - "CS8021" - "CS8027" - "CS8028" From 2b5f78f49f925fac659f43f2d04c5a8bb842beca Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Tue, 10 Feb 2026 14:45:40 -0500 Subject: [PATCH 09/12] Yet one more set of edits --- .../using-directive-errors.md | 502 ++---------------- 1 file changed, 45 insertions(+), 457 deletions(-) diff --git a/docs/csharp/language-reference/compiler-messages/using-directive-errors.md b/docs/csharp/language-reference/compiler-messages/using-directive-errors.md index 0ae3091ea756d..5d7c903f05a7a 100644 --- a/docs/csharp/language-reference/compiler-messages/using-directive-errors.md +++ b/docs/csharp/language-reference/compiler-messages/using-directive-errors.md @@ -102,21 +102,22 @@ This article covers the following compiler errors: -- [**CS0104**](#cs0104): *Error: 'reference' is an ambiguous reference between 'identifier' and 'identifier'.* -- [**CS0116**](#cs0116): *Error: A namespace cannot directly contain members such as fields, methods or statements.* +- [**CS0104**](#namespace-and-type-naming-conflicts): *Error: 'reference' is an ambiguous reference between 'identifier' and 'identifier'.* +- [**CS0116**](#namespace-declarations): *Error: A namespace cannot directly contain members such as fields, methods or statements.* - [**CS0138**](#using-static-directive): *Error: A using namespace directive can only be applied to namespaces; 'type' is a type not a namespace.* - [**CS0430**](#using-directive): *Error: The extern alias 'alias' was not specified in a /reference option.* - [**CS0431**](#alias-qualifier): *Error: Cannot use alias 'identifier' with `::` since the alias references a type. Use `.` instead*. - [**CS0432**](#alias-qualifier): *Error: Alias 'identifier' not found.* -- [**CS0434**](#cs0434): *Error: The namespace NamespaceName1 in NamespaceName2 conflicts with the type TypeName1 in NamespaceName3.* -- [**CS0438**](#cs0438): *Error: The type 'type' in 'module_1' conflicts with the namespace 'namespace' in 'module_2'.* +- [**CS0434**](#namespace-and-type-naming-conflicts): *Error: The namespace NamespaceName1 in NamespaceName2 conflicts with the type TypeName1 in NamespaceName3.* +- [**CS0438**](#namespace-and-type-naming-conflicts): *Error: The type 'type' in 'module_1' conflicts with the namespace 'namespace' in 'module_2'.* - [**CS0439**](#using-directive): *Error: An extern alias declaration must precede all other elements defined in the namespace.* -- [**CS0518**](#cs0518): *Error: Predefined type 'type' is not defined or imported.* +- [**CS0518**](#predefined-type-imports): *Error: Predefined type 'type' is not defined or imported.* - [**CS0576**](#using-alias-restrictions): *Error: Namespace 'namespace' contains a definition conflicting with alias 'identifier'.* - [**CS0687**](#alias-qualifier): *Error: The namespace alias qualifier `::` always resolves to a type or namespace so is illegal here. Consider using `.` instead.* +- [**CS1022**](#namespace-declarations): *Error: Type or namespace definition, or end-of-file expected.* - [**CS1529**](#using-directive): *Error: A using clause must precede all other elements defined in the namespace except extern alias declarations.* - [**CS1537**](#using-alias-restrictions): *Error: The using alias 'alias' appeared previously in this namespace.* -- [**CS1671**](#file-scoped-namespace): *Error: A namespace declaration cannot have modifiers or attributes.* +- [**CS1671**](#namespace-declarations): *Error: A namespace declaration cannot have modifiers or attributes.* - [**CS1679**](#using-directive): *Error: Invalid extern alias for '/reference'; 'identifier' is not a valid identifier.* - [**CS1680**](#using-directive): *Error: Invalid reference alias option: 'alias=' -- missing filename.* - [**CS1681**](#using-directive): *Error: You cannot redefine the global extern alias.* @@ -125,6 +126,7 @@ That's by design. The text closely matches the text of the compiler error / warn - [**CS7000**](#alias-qualifier): *Error: Unexpected use of an aliased name.* - [**CS7007**](#using-static-directive): *Error: A `using static` directive can only be applied to types. Consider a `using namespace` directive instead* - [**CS7015**](#using-directive): *Error: 'extern alias' is not valid in this context.* +- [**CS7021**](#namespace-declarations): *Error: Cannot declare namespace in script code.* - [**CS8083**](#alias-qualifier): *Error: An alias-qualified name is not an expression.* - [**CS8085**](#using-alias-restrictions): *Error: A 'using static' directive cannot be used to declare an alias.* - [**CS8914**](#global-using-directive): *Error: A global using directive cannot be used in a namespace declaration.* @@ -132,8 +134,6 @@ That's by design. The text closely matches the text of the compiler error / warn - [**CS8954**](#file-scoped-namespace): *Error: Source file can only contain one file-scoped namespace declaration.* - [**CS8955**](#file-scoped-namespace): *Error: Source file can not contain both file-scoped and normal namespace declarations.* - [**CS8956**](#file-scoped-namespace): *Error: File-scoped namespace must precede all other members in a file.* -- [**CS1022**](#cs1022): *Error: Type or namespace definition, or end-of-file expected.* -- [**CS7021**](#cs7021): *Error: Cannot declare namespace in script code.* - [**CS9130**](#using-alias-restrictions): *Error: Using alias cannot be a `ref` type.* - [**CS9131**](#using-alias-restrictions): *Error: Only a using alias can be `unsafe`.* - [**CS9132**](#using-alias-restrictions): *Error: Using alias cannot be a nullable reference type.* @@ -143,15 +143,15 @@ That's by design. The text closely matches the text of the compiler error / warn And the following compiler warnings: - [**CS0105**](#using-directive): *Warning: The using directive for 'namespace' appeared previously in this namespace.* +- [**CS0435**](#namespace-and-type-naming-conflicts): *Warning: The namespace 'namespace' in 'assembly' conflicts with the imported type 'type' in 'assembly'. Using the namespace defined in 'assembly'.* +- [**CS0436**](#namespace-and-type-naming-conflicts): *Warning: The type 'type' in 'assembly' conflicts with the imported type 'type2' in 'assembly'. Using the type defined in 'assembly'.* +- [**CS0437**](#namespace-and-type-naming-conflicts): *Warning: The type 'type' in 'assembly2' conflicts with the imported namespace 'namespace' in 'assembly1'. Using the type defined in 'assembly'.* - [**CS0440**](#alias-qualifier): *Warning: Defining an alias named `global` is ill-advised since `global::` always references the global namespace and not an alias.* -- [**CS0435**](#cs0435): *Warning: The namespace 'namespace' in 'assembly' conflicts with the imported type 'type' in 'assembly'. Using the namespace defined in 'assembly'.* -- [**CS0436**](#cs0436): *Warning: The type 'type' in 'assembly' conflicts with the imported type 'type2' in 'assembly'. Using the type defined in 'assembly'.* -- [**CS0437**](#cs0437): *Warning: The type 'type' in 'assembly2' conflicts with the imported namespace 'namespace' in 'assembly1'. Using the type defined in 'assembly'.* - [**CS8019**](#using-directive): *Info: Unnecessary using directive.* - [**CS8020**](#using-directive): *Info: Unused extern alias.* - [**CS8933**](#using-directive): *Info: The using directive appeared previously as global using.* -These errors and warnings indicate your `using` directive isn't formed correctly. The following sections cover these errors and how to correct them. +These errors and warnings indicate issues with `using` directives, namespace declarations, or naming conflicts between types and namespaces. The following sections cover these errors and how to correct them. ## Using directive @@ -171,7 +171,7 @@ The following errors relate to `using` directives: - **CS8020**: *Unused extern alias.* - **CS8933**: *The using directive appeared previously as global using.* -See the [using directive](../keywords/using-directive.md) and [extern alias](../keywords/extern-alias.md) language reference for usage rules. +See the [using directive](../keywords/using-directive.md) and [extern alias](../keywords/extern-alias.md) language reference for the rules that govern these diagnostics. Move all `using` directives to the top of the file, or to the top of the namespace declaration, because the C# language requires `using` directives to come before other elements in a namespace (**CS1529**). Move all `extern alias` declarations before any `using` directives, because the language requires extern aliases to come before all other elements including `using` directives (**CS0439**, **CS7015**). Move all assembly and module level attributes after `using` clauses and `extern alias` declarations but before any type declarations, because attributes must follow directives but precede types (**CS1730**). @@ -188,7 +188,7 @@ The following errors relate to `using static` directives: - **CS9133**: *`static` modifier must precede `unsafe` modifier.* - **CS9162**: *Type is not valid for 'using static'. Only a class, struct, interface, enum, delegate, or namespace can be used.* -See the [using static directive](../keywords/using-directive.md#the-static-modifier) language reference for usage rules. +See the [using static directive](../keywords/using-directive.md#the-static-modifier) language reference for the rules that govern these diagnostics. Add the `static` modifier when importing a type's members directly, because omitting `static` tells the compiler you're importing a namespace rather than a type (**CS0138**). Remove the `static` modifier when importing a namespace, because `using static` can only be applied to types, not namespaces (**CS7007**). Ensure the target of a `using static` directive is a class, struct, interface, enum, or delegate, because other types aren't valid targets for static imports (**CS9162**). Place the `static` modifier before the `unsafe` modifier when combining both, because the language requires modifiers in a specific order (**CS9133**). @@ -199,7 +199,7 @@ The following errors relate to `global using` directives: - **CS8914**: *A global using directive cannot be used in a namespace declaration.* - **CS8915**: *A global using directive must precede all non-global using directives.* -See the [global using directive](../keywords/using-directive.md#the-global-modifier) language reference for usage rules. +See the [global using directive](../keywords/using-directive.md#the-global-modifier) language reference for the rules that govern these diagnostics. Move `global using` directives outside of any namespace declaration to file scope, because global usings apply project-wide and can't be scoped to a namespace (**CS8914**). Place all `global using` directives before any non-global `using` directives in the file, because the language requires global directives to precede local ones (**CS8915**). Note that a `static global using` directive can't reference a [file-local](../keywords/file.md) type. @@ -207,14 +207,13 @@ Move `global using` directives outside of any namespace declaration to file scop The following errors relate to file-scoped namespaces: -- **CS1671**: *A namespace declaration cannot have modifiers or attributes.* - **CS8954**: *Source file can only contain one file-scoped namespace declaration.* - **CS8955**: *Source file can not contain both file-scoped and normal namespace declarations.* - **CS8956**: *File-scoped namespace must precede all other members in a file.* -See the [file-scoped namespace](../keywords/namespace.md) language reference for usage rules. +See the [file-scoped namespace](../keywords/namespace.md) language reference for the rules that govern these diagnostics. -Use only one file-scoped namespace declaration per file, because the language allows only a single file-scoped namespace to set the namespace for all types in a file (**CS8954**). Choose either file-scoped or block-scoped namespace declarations within a single file, because the language doesn't allow mixing both styles (**CS8955**). Move the file-scoped namespace declaration before any type declarations, because the namespace must be established before types are declared (**CS8956**). Remove any access modifiers or attributes from namespace declarations, because namespaces can't have modifiers or attributes applied to them (**CS1671**). +Use only one file-scoped namespace declaration per file, because the language allows only a single file-scoped namespace to set the namespace for all types in a file (**CS8954**). Choose either file-scoped or block-scoped namespace declarations within a single file, because the language doesn't allow mixing both styles (**CS8955**). Move the file-scoped namespace declaration before any type declarations, because the namespace must be established before types are declared (**CS8956**). ## Alias qualifier @@ -227,9 +226,9 @@ The following errors relate to the alias qualifier: - **CS7000**: *Unexpected use of an aliased name.* - **CS8083**: *An alias-qualified name is not an expression.* -See the [namespace alias qualifier](../operators/namespace-alias-qualifier.md) language reference for usage rules. +See the [namespace alias qualifier](../operators/namespace-alias-qualifier.md) language reference for the rules that govern these diagnostics. -Replace the `::` operator with the `.` operator when you access members of a type alias, because the `::` qualifier is only valid for namespace aliases, not type aliases (**CS0431**, **CS0687**). Ensure the alias you're referencing is declared by using a `using` directive or `extern alias`, because the compiler can't resolve an undefined alias (**CS0432**). Use the alias qualifier only in contexts where a type or namespace name is expected, because alias-qualified names aren't valid as expressions (**CS7000**, **CS8083**). Choose a different name for your alias instead of `global`, because `global` is reserved to refer to the global namespace and can't be redefined (**CS0440**). +Replace the `::` operator with the `.` operator when you access members of a type alias, because the `::` qualifier is only valid for namespace aliases, not type aliases (**CS0431**, **CS0687**). Ensure the alias you're referencing is declared with a `using` directive or `extern alias`, because the compiler can't resolve an undefined alias (**CS0432**). Use the alias qualifier only in contexts where a type or namespace name is expected, because alias-qualified names aren't valid as expressions (**CS7000**, **CS8083**). Choose a different name for your alias instead of `global`, because `global` is reserved to refer to the global namespace and can't be redefined (**CS0440**). ## Using alias restrictions @@ -242,461 +241,50 @@ The following errors relate to restrictions on using aliases: - **CS9131**: *Only a using alias can be `unsafe`.* - **CS9132**: *Using alias cannot be a nullable reference type.* -See the [using alias](../keywords/using-directive.md#the-using-alias) language reference for usage rules. +See the [using alias](../keywords/using-directive.md#the-using-alias) language reference for the rules that govern these diagnostics. -Choose a unique name for your alias that doesn't conflict with existing type or namespace names in scope, because the compiler can't distinguish between the alias and the existing definition (**CS0576**). Use each alias name only once within a namespace, because duplicate alias declarations create ambiguity (**CS1537**). Remove the `static` modifier when declaring an alias, because aliases and static imports are mutually exclusive - use either `using static` to import members or `using Alias =` to create an alias, but not both together (**CS8085**). +Choose a unique name for your alias that doesn't conflict with existing type or namespace names in scope, because the compiler can't distinguish between the alias and the existing definition (**CS0576**). Use each alias name only once within a namespace, because duplicate alias declarations create ambiguity (**CS1537**). Remove the `static` modifier when declaring an alias, because aliases and static imports are mutually exclusive—use either `using static` to import members or `using Alias =` to create an alias, but not both together (**CS8085**). Starting with C# 12, the following restrictions apply to using aliases: Don't use `ref`, `in`, or `out` modifiers in a using alias, because these parameter modifiers aren't valid in type alias contexts (**CS9130**). Use the `unsafe` modifier only with aliases that reference pointer types or with `using static` directives, because `unsafe` without an alias or static import isn't permitted (**CS9131**). Use a non-nullable reference type when creating an alias to a reference type, because nullable reference types can't be aliased directly (**CS9132**). -## CS0116 - -A namespace cannot directly contain members such as fields or methods. - -A namespace can contain other namespaces, structs, and classes. For more information, see the [namespace keyword](../keywords/namespace.md) article. - -### Example - -The following sample will cause Visual Studio to flag parts of the code as being in violation of CS0116. Attempting to build this code will result in build failure: - -```csharp -// CS0116.cs -namespace x -{ - // A namespace can be placed within another namespace. - using System; - - // These variables trigger the CS0116 error as they are declared outside of a struct or class. - public int latitude; - public int longitude; - Coordinate coord; - - // Auto-properties also fall under the definition of this rule. - public string LocationName { get; set; } - - // This method as well: if it isn't in a class or a struct, it's violating CS0116. - public void DisplayLatitude() - { - Console.WriteLine($"Lat: {latitude}"); - } +## Namespace declarations - public struct Coordinate - { - } +The following errors relate to namespace declaration rules: - public class CoordinatePrinter - { - public void DisplayLongitude() - { - Console.WriteLine($"Longitude: {longitude}"); - } - - public void DisplayLocation() - { - Console.WriteLine($"Location: {LocationName}"); - } - } -} -``` +- **CS0116**: *A namespace cannot directly contain members such as fields, methods or statements.* +- **CS1022**: *Type or namespace definition, or end-of-file expected.* +- **CS1671**: *A namespace declaration cannot have modifiers or attributes.* +- **CS7021**: *Cannot declare namespace in script code.* -Note that in C#, methods and variables must be declared and defined within a struct or class. For more information on program structure in C#, see the [General Structure of a C# Program](../../fundamentals/program-structure/index.md) article. To fix this error, rewrite your code such that all methods and fields are contained within either a struct or a class: +See the [namespace keyword](../keywords/namespace.md) and [General Structure of a C# Program](../../fundamentals/program-structure/index.md) language reference for the rules that govern these diagnostics. -```csharp -namespace x -{ - // A namespace can be placed within another namespace. - using System; +Ensure all methods, fields, and properties are declared inside a type (class, struct, record, or interface) rather than directly inside a namespace, because namespaces can only contain type declarations, nested namespaces, and `using` directives (**CS0116**). Check for mismatched braces in your source file, because an extra closing brace after a namespace or type definition produces an error when the compiler encounters unexpected content at the end of the file (**CS1022**). Remove any access modifiers or attributes from namespace declarations, because namespaces don't support modifiers like `public` or `private`, and attributes can't be applied to them (**CS1671**). Move namespace declarations out of C# script files (`.csx`) and into regular source files (`.cs`), because script code evaluates in a single execution context that doesn't support namespace declarations (**CS7021**). - // These variables are now placed within a struct, so CS0116 is no longer violated. - public struct Coordinate - { - public int Latitude; - public int Longitude; - } +## Namespace and type naming conflicts - // The methods and fields are now placed within a class, and the compiler is satisfied. - public class CoordinatePrinter - { - Coordinate coord; - public string LocationName { get; set; } +The following errors and warnings relate to naming conflicts between namespaces and types: - public void DisplayLatitude() - { - Console.WriteLine($"Lat: {coord.Latitude}"); - } +- **CS0104**: *'reference' is an ambiguous reference between 'identifier' and 'identifier'.* +- **CS0434**: *The namespace NamespaceName1 in NamespaceName2 conflicts with the type TypeName1 in NamespaceName3.* +- **CS0435**: *The namespace 'namespace' in 'assembly' conflicts with the imported type 'type' in 'assembly'. Using the namespace defined in 'assembly'.* +- **CS0436**: *The type 'type' in 'assembly' conflicts with the imported type 'type2' in 'assembly'. Using the type defined in 'assembly'.* +- **CS0437**: *The type 'type' in 'assembly2' conflicts with the imported namespace 'namespace' in 'assembly1'. Using the type defined in 'assembly'.* +- **CS0438**: *The type 'type' in 'module_1' conflicts with the namespace 'namespace' in 'module_2'.* - public void DisplayLongitude() - { - Console.WriteLine($"Longitude: {coord.Longitude}"); - } +See the [using directive](../keywords/using-directive.md), [extern alias](../keywords/extern-alias.md), and [namespace alias qualifier](../operators/namespace-alias-qualifier.md) language reference for the rules that govern these diagnostics. - public void DisplayLocation() - { - Console.WriteLine($"Location: {LocationName}"); - } - } -} -``` +Use a fully qualified name or a [namespace alias](../operators/namespace-alias-qualifier.md) when your code references a name that exists in multiple imported namespaces, because the compiler can't determine which type you intend to use when the same name appears in two or more namespaces imported by `using` directives (**CS0104**). Rename either the type or the namespace when an imported type and an imported nested namespace share the same fully qualified name, because the compiler can't distinguish between them when the name is referenced (**CS0434**, **CS0438**). -## CS0518 +To resolve the naming conflict warnings, rename one of the conflicting declarations, use a different namespace, remove the unnecessary assembly reference, or use an [extern alias](../keywords/extern-alias.md) to disambiguate between the two definitions. The compiler resolves these conflicts automatically—using the locally defined namespace over the imported type (**CS0435**), the locally defined type over the imported type (**CS0436**), or the locally defined type over the imported namespace (**CS0437**)—but the warnings indicate a potential source of confusion that you should address. -Predefined type 'type' is not defined or imported. +## Predefined type imports -> [!NOTE] -> The resolution for this error depends on whether you're using a modern SDK-style project (`.csproj` files that start with ``) or legacy project formats. SDK-style projects manage runtime references automatically through the `` property. +The following error relates to missing predefined type definitions: -The main cause for this problem is that the project cannot access the predefined types from the .NET runtime library. In modern SDK-style projects, this is typically due to an incorrect or missing `` specification. In legacy projects, this issue is caused by not importing mscorlib.dll, which defines the entire namespace. This can be caused by one of the following: +- **CS0518**: *Predefined type 'type' is not defined or imported.* [!INCLUDE[csharp-build-only-diagnostic-note](~/includes/csharp-build-only-diagnostic-note.md)] -- The [**NoStandardLib**](../compiler-options/advanced.md#nostandardlib) option from the command line compiler has been specified. The **NoStandardLib** option prevents the import of mscorlib.dll. Use this option if you want to define or create a user-specific System namespace. - -- An incorrect mscorlib.dll is referenced. - -- A corrupt Visual Studio .NET or .NET Framework common language runtime installation exists. - -- Residual components from an earlier installation that are incompatible with the latest installation remain. - -To resolve this problem, take one of the following actions: - -- Do not specify the /nostdlib option from the command line compiler. - -- For modern SDK-style projects, ensure the project targets the correct .NET runtime. In your `.csproj` file, verify the `` property specifies the intended runtime: - - ```xml - - net8.0 - - ``` - - For multi-targeting projects, use `` (plural): - - ```xml - - net8.0;net48 - - ``` - -- For legacy project formats, make sure that the project refers to the correct mscorlib.dll. - -- Reinstall the .NET Framework common language runtime (if the previous solutions do not solve the problem). - -- Reload the project in Visual Studio. - -- Close Visual Studio, delete the `obj` and `bin` folders from your project directory, then reopen Visual Studio and rebuild the project. - -## CS0104 - -'reference' is an ambiguous reference between 'identifier' and 'identifier'. - -Your program contains [using](../keywords/using-directive.md) directives for two namespaces and your code references a name that appears in both namespaces. - -The following sample generates CS0104: - -```csharp -// CS0104.cs -using x; -using y; - -namespace x -{ - public class Test - { - } -} - -namespace y -{ - public class Test - { - } -} - -public class a -{ - public static void Main() - { - Test test = new Test(); // CS0104, is Test in x or y namespace? - // try the following line instead - // y.Test test = new y.Test(); - } -} -``` - -## CS0434 - -The namespace NamespaceName1 in NamespaceName2 conflicts with the type TypeName1 in NamespaceName3. - -This error occurs when an imported type and an imported nested namespace have the same fully qualified name. When that name is referenced, the compiler is unable to distinguish between the two. If you can change the imported source code, you can resolve the error by changing the name of either the type or the namespace so that both are unique within the assembly. - -The following code generates error CS0434. - -### Example 1 - -This code creates the first copy of the type with the identical fully qualified name. - -```csharp -// CS0434_1.cs -// compile with: /t:library -namespace TypeBindConflicts -{ - namespace NsImpAggPubImp - { - public class X { } - } -} -``` - -### Example 2 - -This code creates the second copy of the type with the identical fully qualified name. - -```csharp -// CS0434_2.cs -// compile with: /t:library -namespace TypeBindConflicts { - // Conflicts with another import (import2.cs). - public class NsImpAggPubImp { } - // Try this instead: - // public class UniqueClassName { } -} -``` - -### Example 3 - -This code references the type with the identical fully qualified name. - -```csharp -// CS0434.cs -// compile with: /r:cs0434_1.dll /r:cs0434_2.dll -using TypeBindConflicts; -public class Test -{ - public TypeBindConflicts.NsImpAggPubImp.X n2 = null; // CS0434 -} -``` - -## CS0435 - -The namespace 'namespace' in 'assembly' conflicts with the imported type 'type' in 'assembly'. Using the namespace defined in 'assembly'. - -This warning is issued when a namespace in a source file (file_2) conflicts with an imported type in file_1. The compiler uses the one in the source file. - -The following example generates CS0435: - -Compile this file first: - -```csharp -// CS0435_1.cs -// compile with: /t:library -public class Util -{ - public class A { } -} -``` - -Then, compile this file: - -```csharp -// CS0435_2.cs -// compile with: /r:CS0435_1.dll - -using System; - -namespace Util -{ - public class A { } -} - -public class Test -{ - public static void Main() - { - Console.WriteLine(typeof(Util.A)); // CS0435 - } -} -``` - -## CS0436 - -The type 'type' in 'assembly' conflicts with the imported type 'type2' in 'assembly'. Using the type defined in 'assembly'. - -This warning occurs when a type defined in your source code has the same fully qualified name (namespace and type name) as a type imported from a referenced assembly. When this name conflict occurs, the compiler uses the locally defined type from your source file and ignores the imported type. - -### What constitutes a conflict - -A conflict occurs when two types have identical fully qualified names, meaning: - -- They have the same namespace -- They have the same type name -- They're both accessible in the current compilation context - -The conflict is determined solely by the type's fully qualified name, not by its implementation details. Two types with the same name but different implementations (such as different methods, properties, or field values) still conflict. The compiler can't use both types simultaneously because they have the same identity. - -### Example - -The following example demonstrates CS0436. In this scenario, a type `A` is defined in an external library and also locally in the source file. Even though the two types have different implementations (they print different strings), they conflict because they share the same fully qualified name. - -First, create a library that defines type `A`: - -```csharp -// CS0436_a.cs -// compile with: /target:library -public class A { - public void Test() { - System.Console.WriteLine("CS0436_a"); - } -} -``` - -Then, compile the following code that defines another type `A` and references the library. The compiler issues CS0436 because both types have the fully qualified name `A` (in the global namespace): - -```csharp -// CS0436_b.cs -// compile with: /reference:CS0436_a.dll -// CS0436 expected -public class A { - public void Test() { - System.Console.WriteLine("CS0436_b"); - } -} - -public class Test -{ - public static void Main() - { - A x = new A(); - x.Test(); - } -} -``` - -When you compile and run this code, the compiler uses the locally defined `A` (from CS0436_b.cs) and issues a warning. The output is: - -```console -CS0436_b -``` - -Note that the conflict exists even though the two `A` types have different implementations. The difference in the string literal (`"CS0436_a"` versus `"CS0436_b"`) doesn't prevent the conflict. What matters is that both types have the same fully qualified name `A`. - -### How to resolve this warning - -To resolve this warning, you can: - -1. Rename one of the conflicting types. -1. Use a different namespace for one of the types. -1. Remove the reference to the assembly containing the conflicting type if it's not needed. -1. Use an extern alias to disambiguate between the two types if you need to use both (see [CS0433](../language-reference/compiler-messages/cs0433.md) for examples of using extern aliases). - -## CS0437 - -The type 'type' in 'assembly2' conflicts with the imported namespace 'namespace' in 'assembly1'. Using the type defined in 'assembly'. - -This warning is issued when a type in a source file, file_2, conflicts with an imported namespace in file_1. The compiler uses the type in the source file. - -### Example 1 - -```csharp -// CS0437_a.cs -// compile with: /target:library -namespace Util -{ - public class A { - public void Test() { - System.Console.WriteLine("CS0437_a.cs"); - } - } -} -``` - -### Example 2 - -The following sample generates CS0437. - -```csharp -// CS0437_b.cs -// compile with: /reference:CS0437_a.dll /W:2 -// CS0437 expected -class Util -{ - public class A { - public void Test() { - System.Console.WriteLine("CS0437_b.cs"); - } - } -} - -public class Test -{ - public static void Main() - { - Util.A x = new Util.A(); - x.Test(); - } -} -``` - -## CS0438 - -The type 'type' in 'module_1' conflicts with the namespace 'namespace' in 'module_2'. - -This error occurs when a type in a source file conflicts with a namespace in another source file. This typically happens when one or both come from an added module. To resolve, rename the type or the namespace that caused the conflict. - -The following example generates CS0438: - -Compile this file first: - -```csharp -// CS0438_1.cs -// compile with: /target:module -public class Util -{ - public class A { } -} -``` - -Then compile this file: - -```csharp -// CS0438_2.cs -// compile with: /target:module -namespace Util -{ - public class A { } -} -``` - -And then compile this file: +See the [NoStandardLib compiler option](../compiler-options/advanced.md#nostandardlib) language reference for the rules that govern this diagnostic. -```csharp -// CS0438_3.cs -// compile with: /addmodule:CS0438_1.netmodule /addmodule:CS0438_2.netmodule -using System; -public class Test -{ - public static void Main() { - Console.WriteLine(typeof(Util.A)); // CS0438 - } -} -``` - -## CS1022 - -Type or namespace definition, or end-of-file expected. - -A source-code file does not have a matching set of braces. - -The following sample generates CS1022: - -```csharp -// CS1022.cs -namespace x -{ -} -} // CS1022 -``` - -## CS7021 - -You can't declare a namespace in script code. - -C# script files (`.csx`) don't support namespace declarations. All code in a script file is evaluated in a single execution context. If you need to organize code into namespaces, move it into a regular C# source file (`.cs`). +Verify that your project targets the correct .NET runtime, because predefined types like `System.Int32` and `System.String` come from the runtime library and an incorrect or missing `` specification prevents the compiler from finding them (**CS0518**). Ensure the `` property in your `.csproj` file specifies the intended runtime (for example, `net10.0`). Don't specify the [**NoStandardLib**](../compiler-options/advanced.md#nostandardlib) compiler option unless you intend to define your own `System` namespace, because this option prevents importing the standard library that defines all predefined types (**CS0518**). If the error persists, try reloading the project in Visual Studio, deleting the `obj` and `bin` folders and rebuilding, or reinstalling the .NET runtime (**CS0518**). From d87da756418d2e9561fc1529f19ada68fa8f6b44 Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Tue, 10 Feb 2026 14:54:15 -0500 Subject: [PATCH 10/12] Final proofread --- .../compiler-messages/using-directive-errors.md | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/docs/csharp/language-reference/compiler-messages/using-directive-errors.md b/docs/csharp/language-reference/compiler-messages/using-directive-errors.md index 5d7c903f05a7a..41c62a871c766 100644 --- a/docs/csharp/language-reference/compiler-messages/using-directive-errors.md +++ b/docs/csharp/language-reference/compiler-messages/using-directive-errors.md @@ -100,7 +100,7 @@ helpviewer_keywords: This article covers the following compiler errors: - [**CS0104**](#namespace-and-type-naming-conflicts): *Error: 'reference' is an ambiguous reference between 'identifier' and 'identifier'.* - [**CS0116**](#namespace-declarations): *Error: A namespace cannot directly contain members such as fields, methods or statements.* @@ -124,7 +124,7 @@ That's by design. The text closely matches the text of the compiler error / warn - [**CS1730**](#using-directive): *Error: Assembly and module attributes must precede all other elements defined in a file except using clauses and extern alias declarations.* - [**CS2034**](#using-directive): *Error: A /reference option that declares an extern alias can only have one filename. To specify multiple aliases or filenames, use multiple /reference options.* - [**CS7000**](#alias-qualifier): *Error: Unexpected use of an aliased name.* -- [**CS7007**](#using-static-directive): *Error: A `using static` directive can only be applied to types. Consider a `using namespace` directive instead* +- [**CS7007**](#using-static-directive): *Error: A `using static` directive can only be applied to types. Consider a `using namespace` directive instead.* - [**CS7015**](#using-directive): *Error: 'extern alias' is not valid in this context.* - [**CS7021**](#namespace-declarations): *Error: Cannot declare namespace in script code.* - [**CS8083**](#alias-qualifier): *Error: An alias-qualified name is not an expression.* @@ -151,7 +151,7 @@ And the following compiler warnings: - [**CS8020**](#using-directive): *Info: Unused extern alias.* - [**CS8933**](#using-directive): *Info: The using directive appeared previously as global using.* -These errors and warnings indicate issues with `using` directives, namespace declarations, or naming conflicts between types and namespaces. The following sections cover these errors and how to correct them. +These errors and warnings indicate problems with `using` directives, namespace declarations, or naming conflicts between types and namespaces. The following sections describe these errors and how to correct them. ## Using directive @@ -243,7 +243,7 @@ The following errors relate to restrictions on using aliases: See the [using alias](../keywords/using-directive.md#the-using-alias) language reference for the rules that govern these diagnostics. -Choose a unique name for your alias that doesn't conflict with existing type or namespace names in scope, because the compiler can't distinguish between the alias and the existing definition (**CS0576**). Use each alias name only once within a namespace, because duplicate alias declarations create ambiguity (**CS1537**). Remove the `static` modifier when declaring an alias, because aliases and static imports are mutually exclusive—use either `using static` to import members or `using Alias =` to create an alias, but not both together (**CS8085**). +Choose a unique name for your alias that doesn't conflict with existing type or namespace names in scope, because the compiler can't distinguish between the alias and the existing definition (**CS0576**). Use each alias name only once within a namespace, because duplicate alias declarations create ambiguity (**CS1537**). Remove the `static` modifier when declaring an alias, because aliases and static imports are mutually exclusive - use either `using static` to import members or `using Alias =` to create an alias, but not both together (**CS8085**). Starting with C# 12, the following restrictions apply to using aliases: Don't use `ref`, `in`, or `out` modifiers in a using alias, because these parameter modifiers aren't valid in type alias contexts (**CS9130**). Use the `unsafe` modifier only with aliases that reference pointer types or with `using static` directives, because `unsafe` without an alias or static import isn't permitted (**CS9131**). Use a non-nullable reference type when creating an alias to a reference type, because nullable reference types can't be aliased directly (**CS9132**). @@ -273,9 +273,9 @@ The following errors and warnings relate to naming conflicts between namespaces See the [using directive](../keywords/using-directive.md), [extern alias](../keywords/extern-alias.md), and [namespace alias qualifier](../operators/namespace-alias-qualifier.md) language reference for the rules that govern these diagnostics. -Use a fully qualified name or a [namespace alias](../operators/namespace-alias-qualifier.md) when your code references a name that exists in multiple imported namespaces, because the compiler can't determine which type you intend to use when the same name appears in two or more namespaces imported by `using` directives (**CS0104**). Rename either the type or the namespace when an imported type and an imported nested namespace share the same fully qualified name, because the compiler can't distinguish between them when the name is referenced (**CS0434**, **CS0438**). +Use a fully qualified name or a [namespace alias](../operators/namespace-alias-qualifier.md) when your code references a name that exists in multiple imported namespaces. The compiler can't determine which type you intend to use when the same name appears in two or more namespaces imported by `using` directives (**CS0104**). Rename either the type or the namespace when an imported type and an imported nested namespace share the same fully qualified name. The compiler can't distinguish between them when the name is referenced (**CS0434**, **CS0438**). -To resolve the naming conflict warnings, rename one of the conflicting declarations, use a different namespace, remove the unnecessary assembly reference, or use an [extern alias](../keywords/extern-alias.md) to disambiguate between the two definitions. The compiler resolves these conflicts automatically—using the locally defined namespace over the imported type (**CS0435**), the locally defined type over the imported type (**CS0436**), or the locally defined type over the imported namespace (**CS0437**)—but the warnings indicate a potential source of confusion that you should address. +To resolve the naming conflict warnings, rename one of the conflicting declarations, use a different namespace, remove the unnecessary assembly reference, or use an [extern alias](../keywords/extern-alias.md) to disambiguate between the two definitions. The compiler resolves these conflicts automatically - using the locally defined namespace over the imported type (**CS0435**), the locally defined type over the imported type (**CS0436**), or the locally defined type over the imported namespace (**CS0437**) - but the warnings indicate a potential source of confusion that you should address. ## Predefined type imports @@ -287,4 +287,4 @@ The following error relates to missing predefined type definitions: See the [NoStandardLib compiler option](../compiler-options/advanced.md#nostandardlib) language reference for the rules that govern this diagnostic. -Verify that your project targets the correct .NET runtime, because predefined types like `System.Int32` and `System.String` come from the runtime library and an incorrect or missing `` specification prevents the compiler from finding them (**CS0518**). Ensure the `` property in your `.csproj` file specifies the intended runtime (for example, `net10.0`). Don't specify the [**NoStandardLib**](../compiler-options/advanced.md#nostandardlib) compiler option unless you intend to define your own `System` namespace, because this option prevents importing the standard library that defines all predefined types (**CS0518**). If the error persists, try reloading the project in Visual Studio, deleting the `obj` and `bin` folders and rebuilding, or reinstalling the .NET runtime (**CS0518**). +Verify that your project targets the correct .NET runtime. Predefined types like `System.Int32` and `System.String` come from the runtime library. An incorrect or missing `` specification prevents the compiler from finding them (**CS0518**). Ensure the `` property in your `.csproj` file specifies the intended runtime (for example, `net10.0`). Don't specify the [**NoStandardLib**](../compiler-options/advanced.md#nostandardlib) compiler option unless you intend to define your own `System` namespace. This option prevents importing the standard library that defines all predefined types (**CS0518**). If the error persists, try reloading the project in Visual Studio, deleting the `obj` and `bin` folders and rebuilding, or reinstalling the .NET runtime (**CS0518**). From 76d78f55a28669632c2060af4f985ee85178fec6 Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Tue, 10 Feb 2026 15:17:02 -0500 Subject: [PATCH 11/12] One more copyedit --- .../using-directive-errors.md | 70 +++++++++++++------ 1 file changed, 47 insertions(+), 23 deletions(-) diff --git a/docs/csharp/language-reference/compiler-messages/using-directive-errors.md b/docs/csharp/language-reference/compiler-messages/using-directive-errors.md index 41c62a871c766..1247928fce7dc 100644 --- a/docs/csharp/language-reference/compiler-messages/using-directive-errors.md +++ b/docs/csharp/language-reference/compiler-messages/using-directive-errors.md @@ -171,13 +171,17 @@ The following errors relate to `using` directives: - **CS8020**: *Unused extern alias.* - **CS8933**: *The using directive appeared previously as global using.* -See the [using directive](../keywords/using-directive.md) and [extern alias](../keywords/extern-alias.md) language reference for the rules that govern these diagnostics. - -Move all `using` directives to the top of the file, or to the top of the namespace declaration, because the C# language requires `using` directives to come before other elements in a namespace (**CS1529**). Move all `extern alias` declarations before any `using` directives, because the language requires extern aliases to come before all other elements including `using` directives (**CS0439**, **CS7015**). Move all assembly and module level attributes after `using` clauses and `extern alias` declarations but before any type declarations, because attributes must follow directives but precede types (**CS1730**). - -Ensure that every `extern alias` declaration in your source code has a corresponding alias defined in your project's [reference options](../compiler-options/inputs.md#references), because the compiler can't resolve an alias that wasn't specified (**CS0430**). Use a separate `/reference` option for each extern alias rather than combining multiple aliases in a single option, because the compiler requires one alias per reference option (**CS2034**). Ensure the alias in your `/reference` option is a valid C# identifier, because the alias must follow identifier naming rules (**CS1679**). Include a filename after the `=` sign in your alias reference option, because the compiler needs to know which assembly the alias refers to (**CS1680**). Don't attempt to redefine the `global` extern alias, because `global` is a predefined alias that refers to all unaliased references (**CS1681**). - -Remove duplicate `using` directives, because the compiler warns when the same namespace is imported multiple times (**CS0105**, **CS8019**, **CS8933**). Remove unused `extern alias` declarations, because the compiler issues a diagnostic when an extern alias is declared but never referenced in your code (**CS8020**). +See the [using directive](../keywords/using-directive.md) and [extern alias](../keywords/extern-alias.md) language reference for the rules that govern these diagnostics. Potential fixes include: + +- Move all `using` directives to the top of the file, or to the top of the namespace declaration. The C# language requires `using` directives to come before other elements in a namespace (**CS1529**). +- Move all `extern alias` declarations before any `using` directives. The language requires extern aliases to come before all other elements, including `using` directives (**CS0439**, **CS7015**). +- Move all assembly and module level attributes after `using` clauses and `extern alias` declarations but before any type declarations. Attributes must follow directives but precede types (**CS1730**). +- Ensure that every `extern alias` declaration in your source code has a corresponding alias defined in your project's [reference options](../compiler-options/inputs.md#references). The compiler can't resolve an alias that wasn't specified (**CS0430**). +- Use a separate `/reference` option for each extern alias rather than combining multiple aliases in a single option. The compiler requires one alias per reference option (**CS2034**). +- Ensure the alias in your `/reference` option is a valid C# identifier. The alias must follow identifier naming rules (**CS1679**). Include a filename after the `=` sign in your alias reference option. The compiler needs to know which assembly the alias refers to (**CS1680**). +- Don't attempt to redefine the `global` extern alias. `global` is a predefined alias that refers to all unaliased references (**CS1681**). +- Remove duplicate `using` directives. The compiler warns when the same namespace is imported multiple times (**CS0105**, **CS8019**, **CS8933**). +- Remove unused `extern alias` declarations. The compiler issues a diagnostic when an extern alias is declared but never referenced in your code (**CS8020**). ## Using static directive @@ -188,9 +192,11 @@ The following errors relate to `using static` directives: - **CS9133**: *`static` modifier must precede `unsafe` modifier.* - **CS9162**: *Type is not valid for 'using static'. Only a class, struct, interface, enum, delegate, or namespace can be used.* -See the [using static directive](../keywords/using-directive.md#the-static-modifier) language reference for the rules that govern these diagnostics. +See the [using static directive](../keywords/using-directive.md#the-static-modifier) language reference for the rules that govern these diagnostics. Potential fixes include: -Add the `static` modifier when importing a type's members directly, because omitting `static` tells the compiler you're importing a namespace rather than a type (**CS0138**). Remove the `static` modifier when importing a namespace, because `using static` can only be applied to types, not namespaces (**CS7007**). Ensure the target of a `using static` directive is a class, struct, interface, enum, or delegate, because other types aren't valid targets for static imports (**CS9162**). Place the `static` modifier before the `unsafe` modifier when combining both, because the language requires modifiers in a specific order (**CS9133**). +- Add the `static` modifier when importing a type's members directly, because omitting `static` tells the compiler you're importing a namespace rather than a type (**CS0138**). +- Remove the `static` modifier when importing a namespace, because `using static` can only be applied to types, not namespaces (**CS7007**). Ensure the target of a `using static` directive is a class, struct, interface, enum, or delegate, because other types aren't valid targets for static imports (**CS9162**). +- Place the `static` modifier before the `unsafe` modifier when combining both, because the language requires modifiers in a specific order (**CS9133**). ## Global using directive @@ -199,9 +205,11 @@ The following errors relate to `global using` directives: - **CS8914**: *A global using directive cannot be used in a namespace declaration.* - **CS8915**: *A global using directive must precede all non-global using directives.* -See the [global using directive](../keywords/using-directive.md#the-global-modifier) language reference for the rules that govern these diagnostics. +See the [global using directive](../keywords/using-directive.md#the-global-modifier) language reference for the rules that govern these diagnostics. Potential fixes include: -Move `global using` directives outside of any namespace declaration to file scope, because global usings apply project-wide and can't be scoped to a namespace (**CS8914**). Place all `global using` directives before any non-global `using` directives in the file, because the language requires global directives to precede local ones (**CS8915**). Note that a `static global using` directive can't reference a [file-local](../keywords/file.md) type. +- Move `global using` directives outside of any namespace declaration to file scope, because global usings apply project-wide and can't be scoped to a namespace (**CS8914**). +- Place all `global using` directives before any non-global `using` directives in the file, because the language requires global directives to precede local ones (**CS8915**). +- Note that a `static global using` directive can't reference a [file-local](../keywords/file.md) type. ## File-scoped namespace @@ -211,9 +219,11 @@ The following errors relate to file-scoped namespaces: - **CS8955**: *Source file can not contain both file-scoped and normal namespace declarations.* - **CS8956**: *File-scoped namespace must precede all other members in a file.* -See the [file-scoped namespace](../keywords/namespace.md) language reference for the rules that govern these diagnostics. +See the [file-scoped namespace](../keywords/namespace.md) language reference for the rules that govern these diagnostics. Potential fixes include: -Use only one file-scoped namespace declaration per file, because the language allows only a single file-scoped namespace to set the namespace for all types in a file (**CS8954**). Choose either file-scoped or block-scoped namespace declarations within a single file, because the language doesn't allow mixing both styles (**CS8955**). Move the file-scoped namespace declaration before any type declarations, because the namespace must be established before types are declared (**CS8956**). +- Use only one file-scoped namespace declaration per file, because the language allows only a single file-scoped namespace to set the namespace for all types in a file (**CS8954**). +- Choose either file-scoped or block-scoped namespace declarations within a single file, because the language doesn't allow mixing both styles (**CS8955**). +- Move the file-scoped namespace declaration before any type declarations, because the namespace must be established before types are declared (**CS8956**). ## Alias qualifier @@ -226,9 +236,12 @@ The following errors relate to the alias qualifier: - **CS7000**: *Unexpected use of an aliased name.* - **CS8083**: *An alias-qualified name is not an expression.* -See the [namespace alias qualifier](../operators/namespace-alias-qualifier.md) language reference for the rules that govern these diagnostics. +See the [namespace alias qualifier](../operators/namespace-alias-qualifier.md) language reference for the rules that govern these diagnostics. Potential fixes include: -Replace the `::` operator with the `.` operator when you access members of a type alias, because the `::` qualifier is only valid for namespace aliases, not type aliases (**CS0431**, **CS0687**). Ensure the alias you're referencing is declared with a `using` directive or `extern alias`, because the compiler can't resolve an undefined alias (**CS0432**). Use the alias qualifier only in contexts where a type or namespace name is expected, because alias-qualified names aren't valid as expressions (**CS7000**, **CS8083**). Choose a different name for your alias instead of `global`, because `global` is reserved to refer to the global namespace and can't be redefined (**CS0440**). +- Replace the `::` operator with the `.` operator when you access members of a type alias, because the `::` qualifier is only valid for namespace aliases, not type aliases (**CS0431**, **CS0687**). +- Ensure the alias you're referencing is declared with a `using` directive or `extern alias`, because the compiler can't resolve an undefined alias (**CS0432**). +- Use the alias qualifier only in contexts where a type or namespace name is expected, because alias-qualified names aren't valid as expressions (**CS7000**, **CS8083**). +- Choose a different name for your alias instead of `global`, because `global` is reserved to refer to the global namespace and can't be redefined (**CS0440**). ## Using alias restrictions @@ -241,11 +254,17 @@ The following errors relate to restrictions on using aliases: - **CS9131**: *Only a using alias can be `unsafe`.* - **CS9132**: *Using alias cannot be a nullable reference type.* -See the [using alias](../keywords/using-directive.md#the-using-alias) language reference for the rules that govern these diagnostics. +See the [using alias](../keywords/using-directive.md#the-using-alias) language reference for the rules that govern these diagnostics. Potential fixes include: + +- Choose a unique name for your alias that doesn't conflict with existing type or namespace names in scope, because the compiler can't distinguish between the alias and the existing definition (**CS0576**). +- Use each alias name only once within a namespace, because duplicate alias declarations create ambiguity (**CS1537**). +- Remove the `static` modifier when declaring an alias, because aliases and static imports are mutually exclusive - use either `using static` to import members or `using Alias =` to create an alias, but not both together (**CS8085**). -Choose a unique name for your alias that doesn't conflict with existing type or namespace names in scope, because the compiler can't distinguish between the alias and the existing definition (**CS0576**). Use each alias name only once within a namespace, because duplicate alias declarations create ambiguity (**CS1537**). Remove the `static` modifier when declaring an alias, because aliases and static imports are mutually exclusive - use either `using static` to import members or `using Alias =` to create an alias, but not both together (**CS8085**). +Starting with C# 12, the following restrictions apply to using aliases: -Starting with C# 12, the following restrictions apply to using aliases: Don't use `ref`, `in`, or `out` modifiers in a using alias, because these parameter modifiers aren't valid in type alias contexts (**CS9130**). Use the `unsafe` modifier only with aliases that reference pointer types or with `using static` directives, because `unsafe` without an alias or static import isn't permitted (**CS9131**). Use a non-nullable reference type when creating an alias to a reference type, because nullable reference types can't be aliased directly (**CS9132**). +- Don't use `ref`, `in`, or `out` modifiers in a using alias, because these parameter modifiers aren't valid in type alias contexts (**CS9130**). +- Use the `unsafe` modifier only with aliases that reference pointer types or with `using static` directives, because `unsafe` without an alias or static import isn't permitted (**CS9131**). +- Use a non-nullable reference type when creating an alias to a reference type, because nullable reference types can't be aliased directly (**CS9132**). ## Namespace declarations @@ -256,9 +275,12 @@ The following errors relate to namespace declaration rules: - **CS1671**: *A namespace declaration cannot have modifiers or attributes.* - **CS7021**: *Cannot declare namespace in script code.* -See the [namespace keyword](../keywords/namespace.md) and [General Structure of a C# Program](../../fundamentals/program-structure/index.md) language reference for the rules that govern these diagnostics. +See the [namespace keyword](../keywords/namespace.md) and [General Structure of a C# Program](../../fundamentals/program-structure/index.md) language reference for the rules that govern these diagnostics. Potential fixes include: -Ensure all methods, fields, and properties are declared inside a type (class, struct, record, or interface) rather than directly inside a namespace, because namespaces can only contain type declarations, nested namespaces, and `using` directives (**CS0116**). Check for mismatched braces in your source file, because an extra closing brace after a namespace or type definition produces an error when the compiler encounters unexpected content at the end of the file (**CS1022**). Remove any access modifiers or attributes from namespace declarations, because namespaces don't support modifiers like `public` or `private`, and attributes can't be applied to them (**CS1671**). Move namespace declarations out of C# script files (`.csx`) and into regular source files (`.cs`), because script code evaluates in a single execution context that doesn't support namespace declarations (**CS7021**). +- Ensure all methods, fields, and properties are declared inside a type (class, struct, record, or interface) rather than directly inside a namespace, because namespaces can only contain type declarations, nested namespaces, and `using` directives (**CS0116**). +- Check for mismatched braces in your source file, because an extra closing brace after a namespace or type definition produces an error when the compiler encounters unexpected content at the end of the file (**CS1022**). +- Remove any access modifiers or attributes from namespace declarations, because namespaces don't support modifiers like `public` or `private`, and attributes can't be applied to them (**CS1671**). +- Move namespace declarations out of C# script files (`.csx`) and into regular source files (`.cs`), because script code evaluates in a single execution context that doesn't support namespace declarations (**CS7021**). ## Namespace and type naming conflicts @@ -271,9 +293,11 @@ The following errors and warnings relate to naming conflicts between namespaces - **CS0437**: *The type 'type' in 'assembly2' conflicts with the imported namespace 'namespace' in 'assembly1'. Using the type defined in 'assembly'.* - **CS0438**: *The type 'type' in 'module_1' conflicts with the namespace 'namespace' in 'module_2'.* -See the [using directive](../keywords/using-directive.md), [extern alias](../keywords/extern-alias.md), and [namespace alias qualifier](../operators/namespace-alias-qualifier.md) language reference for the rules that govern these diagnostics. +See the [using directive](../keywords/using-directive.md), [extern alias](../keywords/extern-alias.md), and [namespace alias qualifier](../operators/namespace-alias-qualifier.md) language reference for the rules that govern these diagnostics. Potential fixes include: -Use a fully qualified name or a [namespace alias](../operators/namespace-alias-qualifier.md) when your code references a name that exists in multiple imported namespaces. The compiler can't determine which type you intend to use when the same name appears in two or more namespaces imported by `using` directives (**CS0104**). Rename either the type or the namespace when an imported type and an imported nested namespace share the same fully qualified name. The compiler can't distinguish between them when the name is referenced (**CS0434**, **CS0438**). +- Use a fully qualified name or a [namespace alias](../operators/namespace-alias-qualifier.md) when your code references a name that exists in multiple imported namespaces. +- The compiler can't determine which type you intend to use when the same name appears in two or more namespaces imported by `using` directives (**CS0104**). +- Rename either the type or the namespace when an imported type and an imported nested namespace share the same fully qualified name. The compiler can't distinguish between them when the name is referenced (**CS0434**, **CS0438**). To resolve the naming conflict warnings, rename one of the conflicting declarations, use a different namespace, remove the unnecessary assembly reference, or use an [extern alias](../keywords/extern-alias.md) to disambiguate between the two definitions. The compiler resolves these conflicts automatically - using the locally defined namespace over the imported type (**CS0435**), the locally defined type over the imported type (**CS0436**), or the locally defined type over the imported namespace (**CS0437**) - but the warnings indicate a potential source of confusion that you should address. @@ -287,4 +311,4 @@ The following error relates to missing predefined type definitions: See the [NoStandardLib compiler option](../compiler-options/advanced.md#nostandardlib) language reference for the rules that govern this diagnostic. -Verify that your project targets the correct .NET runtime. Predefined types like `System.Int32` and `System.String` come from the runtime library. An incorrect or missing `` specification prevents the compiler from finding them (**CS0518**). Ensure the `` property in your `.csproj` file specifies the intended runtime (for example, `net10.0`). Don't specify the [**NoStandardLib**](../compiler-options/advanced.md#nostandardlib) compiler option unless you intend to define your own `System` namespace. This option prevents importing the standard library that defines all predefined types (**CS0518**). If the error persists, try reloading the project in Visual Studio, deleting the `obj` and `bin` folders and rebuilding, or reinstalling the .NET runtime (**CS0518**). +Verify that your project targets the correct .NET runtime. Predefined types like `System.Int32` and `System.String` come from the runtime library. An incorrect or missing `` specification prevents the compiler from finding these types (**CS0518**). Ensure the `` property in your `.csproj` file specifies the intended runtime (for example, `net10.0`). Don't specify the [**NoStandardLib**](../compiler-options/advanced.md#nostandardlib) compiler option unless you intend to define your own `System` namespace. This option prevents importing the standard library that defines all predefined types (**CS0518**). If the error persists, try reloading the project in Visual Studio, deleting the `obj` and `bin` folders and rebuilding the project, or reinstalling the .NET runtime (**CS0518**). From 55d9d9e9968a992efcc5f6365c849af683eae1b3 Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Tue, 10 Feb 2026 17:41:09 -0500 Subject: [PATCH 12/12] Apply suggestions from code review --- .../compiler-messages/using-directive-errors.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/csharp/language-reference/compiler-messages/using-directive-errors.md b/docs/csharp/language-reference/compiler-messages/using-directive-errors.md index 1247928fce7dc..b4485b08f7b55 100644 --- a/docs/csharp/language-reference/compiler-messages/using-directive-errors.md +++ b/docs/csharp/language-reference/compiler-messages/using-directive-errors.md @@ -262,8 +262,8 @@ See the [using alias](../keywords/using-directive.md#the-using-alias) language r Starting with C# 12, the following restrictions apply to using aliases: -- Don't use `ref`, `in`, or `out` modifiers in a using alias, because these parameter modifiers aren't valid in type alias contexts (**CS9130**). -- Use the `unsafe` modifier only with aliases that reference pointer types or with `using static` directives, because `unsafe` without an alias or static import isn't permitted (**CS9131**). +- Don't use `ref`, `in`, or `out` modifiers in a using alias, because these parameter modifiers aren't valid in type alias contexts (**CS9130**). +- Use the `unsafe` modifier only with aliases that reference pointer types or with `using static` directives, because `unsafe` without an alias or static import isn't permitted (**CS9131**). - Use a non-nullable reference type when creating an alias to a reference type, because nullable reference types can't be aliased directly (**CS9132**). ## Namespace declarations