diff --git a/.openpublishing.redirection.csharp.json b/.openpublishing.redirection.csharp.json index 4706854645830..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" @@ -1993,10 +2029,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 +2681,26 @@ "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/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" + }, { "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 +2737,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 +2789,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/core/compatibility/windows-forms/10.0/menuitem-contextmenu.md b/docs/core/compatibility/windows-forms/10.0/menuitem-contextmenu.md index 7a48dc2858196..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-name-conflicts) 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/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/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 c52b701cfc5c9..b4485b08f7b55 100644 --- a/docs/csharp/language-reference/compiler-messages/using-directive-errors.md +++ b/docs/csharp/language-reference/compiler-messages/using-directive-errors.md @@ -1,205 +1,314 @@ --- 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/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" + - "CS1679" + - "CS1680" + - "CS1681" + - "CS1730" + - "CS2034" - "CS7000" - "CS7007" + - "CS7015" + - "CS7021" - "CS8019" + - "CS8020" - "CS8083" - "CS8085" - "CS8914" - "CS8915" - "CS8933" + - "CS8954" + - "CS8955" + - "CS8956" - "CS9130" - "CS9131" - "CS9132" - "CS9133" - "CS9162" - - "CS9163" helpviewer_keywords: + - "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" + - "CS7021" - "CS8019" + - "CS8020" - "CS8083" - "CS8085" - "CS8914" - "CS8915" - "CS8933" + - "CS8954" + - "CS8955" + - "CS8956" - "CS9130" - "CS9131" - "CS9132" - "CS9133" - "CS9162" - - "CS9163" --- -# Resolve warnings related using namespaces +# Resolve warnings related to using and declaring namespaces 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.* - [**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.* -- [**CS0576**](#alias-name-conflicts): *Error: Namespace 'namespace' contains a definition conflicting with alias 'identifier'.* +- [**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**](#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**](#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**](#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.* +- [**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.* -- [**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.* -- [**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.* +- [**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**](#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.* 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.* - [**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 you're `using` directive isn't formed correctly. 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 -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: + +- **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.* +- **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. 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**). -```csharp -namespace UsingDirective; -public class MyClass -{ -} +## Using static directive -using System.Text.Json; // CS1529 -``` +The following errors relate to `using static` directives: -To fix this issue, move any `using` declarations to the top of the file or the top of the namespace: +- **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.* -:::code language="csharp" source="./snippets/UsingDirectives/MyClass.cs" id="UsingExample"::: +See the [using static directive](../keywords/using-directive.md#the-static-modifier) language reference for the rules that govern these diagnostics. Potential fixes include: -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. +- 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**). -Incorrectly combining a `using` directive with the `static`, `global`, or `unsafe` modifiers on a `using` directive are covered later in this article. +## Global using directive -## Using static directive +The following errors relate to `global using` directives: -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: +- **CS8914**: *A global using directive cannot be used in a namespace declaration.* +- **CS8915**: *A global using directive must precede all non-global using directives.* -:::code language="csharp" source="./snippets/UsingDirectives/Program.cs" id="UsingStatic"::: +See the [global using directive](../keywords/using-directive.md#the-global-modifier) language reference for the rules that govern these diagnostics. Potential fixes include: -The compiler generates **CS0138** if you omit the `static` modifier: +- 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. -```csharp -using System.Console; // CS0138 -``` +## File-scoped namespace -The compiler generates **CS7007** if you include the `static` modifier importing namespace instead of a type: +The following errors relate to file-scoped namespaces: -```csharp -using static System; // CS7007 -``` +- **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.* -The compiler emits CS9162 if the symbol isn't one of the proper types. +See the [file-scoped namespace](../keywords/namespace.md) language reference for the rules that govern these diagnostics. Potential fixes include: -If you combine the `static` modifier with the `unsafe` modifier in a `using` directive, the `static` modifier must come first: +- 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**). -:::code language="csharp" source="./snippets/UsingDirectives/Program.cs" id="UsingUnsafeStatic"::: +## Alias qualifier -## Global using directive +The following errors relate to the alias qualifier: -A `global using` directive imports the namespace or type in all source files in the current project: +- **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.* -:::code language="csharp" source="./snippets/UsingDirectives/Program.cs" id="GlobalUsing"::: +See the [namespace alias qualifier](../operators/namespace-alias-qualifier.md) language reference for the rules that govern these diagnostics. Potential fixes include: -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. +- 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**). -Furthermore, a `static global using` directive can't reference a [file-local](../keywords/file.md) type. +## Using alias restrictions -## Alias qualifier +The following errors relate to restrictions on using aliases: + +- **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.* + +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**). -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**. +Starting with C# 12, the following restrictions apply to using aliases: -In all cases, replace the `::` with the `.` separator. +- 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**). -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. +## Namespace declarations -## Alias name conflicts +The following errors relate to namespace declaration rules: -You can declare an [alias](../keywords/using-directive.md#the-using-alias) to a namespace or a type with a `using` directive: +- **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.* -:::code language="csharp" source="./snippets/UsingDirectives/Program.cs" id="UsingAlias"::: +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: -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**. +- 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**). -## Restrictions on using aliases +## Namespace and type naming conflicts -Prior to C# 12, the language imposed these restrictions on `using` directives that create an alias for a type declaration: +The following errors and warnings relate to naming conflicts between namespaces and types: -- You can't create an alias with a `using static` directive: +- **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'.* - ```csharp - using static con = System.Console; - using static unsafe ip = int*; - ``` +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: -Beginning with C# 12, these restrictions are introduced: +- 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**). -- You can't use the `in`, `ref`, or `out` modifiers in a using alias: +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. - ```csharp - // All these are invalid - using RefInt = ref int; - using OutInt = out int; - using InInt = in int; - ``` +## Predefined type imports -- An `unsafe using` directive must specify an alias, or a `static using`: +The following error relates to missing predefined type definitions: - ```csharp - // Elsewhere: - public namespace UnsafeExamples - { - public unsafe static class UnsafeType - { - // ... - } - } +- **CS0518**: *Predefined type 'type' is not defined or imported.* - // Using directives: - using unsafe IntPointer = int*; - using static unsafe UnsafeExamples.UnsafeType; - using unsafe UnsafeExamples; // not allowed - ``` +[!INCLUDE[csharp-build-only-diagnostic-note](~/includes/csharp-build-only-diagnostic-note.md)] -- You can't create an alias to a nullable reference type: +See the [NoStandardLib compiler option](../compiler-options/advanced.md#nostandardlib) language reference for the rules that govern this diagnostic. - ```csharp - using NullableInt = System.Int32?; // Allowed - using NullableString = System.String?; // Not allowed - ``` +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**). diff --git a/docs/csharp/language-reference/toc.yml b/docs/csharp/language-reference/toc.yml index 9777c43856cf1..6afa637dc13dd 100644 --- a/docs/csharp/language-reference/toc.yml +++ b/docs/csharp/language-reference/toc.yml @@ -648,9 +648,11 @@ 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, CS9130, CS9131, - CS9132, CS9133, CS9162, CS9163 + 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, + 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 displayName: > @@ -814,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 @@ -826,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 @@ -1010,16 +1008,8 @@ 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 @@ -1066,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 @@ -1320,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 @@ -1526,8 +1512,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 +1522,6 @@ 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 +1552,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 +1670,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 @@ -1970,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/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/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/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/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/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/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/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; -``` 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..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 @@ -80,13 +80,11 @@ f1_keywords: - "CS7006" - "CS7012" - "CS7013" - - "CS7015" - "CS7016" - "CS7017" - "CS7018" - "CS7019" - "CS7020" - - "CS7021" - "CS7022" # build only diagnostic - "CS7024" - "CS7025" @@ -159,7 +157,6 @@ f1_keywords: - "CS8016" - "CS8017" - "CS8018" - - "CS8020" - "CS8021" - "CS8027" - "CS8028" @@ -427,9 +424,6 @@ f1_keywords: - "CS8935" - "CS8937" - "CS8940" - - "CS8954" - - "CS8955" - - "CS8956" - "CS8959" - "CS8960" - "CS8961"