-
Notifications
You must be signed in to change notification settings - Fork 6k
Clarify what constitutes a type conflict in CS0436 compiler warning documentation #48863
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Copilot
wants to merge
2
commits into
main
Choose a base branch
from
copilot/fix-7ddf113a-8a6f-4fb3-aa1c-89f047ae4a86
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+31
−8
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,7 +1,7 @@ | ||||||
--- | ||||||
description: "Compiler Warning (level 2) CS0436" | ||||||
title: "Compiler Warning (level 2) CS0436" | ||||||
ms.date: 07/20/2015 | ||||||
ms.date: 10/01/2025 | ||||||
f1_keywords: | ||||||
- "CS0436" | ||||||
helpviewer_keywords: | ||||||
|
@@ -12,9 +12,23 @@ ms.assetid: c4135d9d-3511-4bbc-9540-48c2091f869c | |||||
|
||||||
The type 'type' in 'assembly' conflicts with the imported type 'type2' in 'assembly'. Using the type defined in 'assembly'. | ||||||
|
||||||
This warning is issued when a type in a source file (file_2) conflicts with an imported type in file_1. The compiler uses the one in the source file. | ||||||
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 are 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 cannot use both types simultaneously because they have the same identity. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
## Example 1 | ||||||
## Example | ||||||
|
||||||
The following examples demonstrate 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. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
First, create a library that defines type `A`: | ||||||
|
||||||
```csharp | ||||||
// CS0436_a.cs | ||||||
|
@@ -26,10 +40,8 @@ public class A { | |||||
} | ||||||
``` | ||||||
|
||||||
## Example 2 | ||||||
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): | ||||||
|
||||||
The following example generates CS0436. | ||||||
|
||||||
```csharp | ||||||
// CS0436_b.cs | ||||||
// compile with: /reference:CS0436_a.dll | ||||||
|
@@ -39,7 +51,7 @@ public class A { | |||||
System.Console.WriteLine("CS0436_b"); | ||||||
} | ||||||
} | ||||||
|
||||||
public class Test | ||||||
{ | ||||||
public static void Main() | ||||||
|
@@ -50,8 +62,19 @@ public class Test | |||||
} | ||||||
``` | ||||||
|
||||||
Compilation produces the following output: | ||||||
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](/dotnet/csharp/language-reference/compiler-messages/cs0433) for examples of using extern aliases). |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.