Skip to content

Commit 38ac9aa

Browse files
CopilotBillWagner
andauthored
Update CS0433 documentation to prioritize extern alias over csc compiler options (#47307)
* Initial plan * Update CS0433 documentation to prioritize extern alias over csc compiler options Co-authored-by: BillWagner <[email protected]> --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: BillWagner <[email protected]>
1 parent 9fa88f9 commit 38ac9aa

File tree

1 file changed

+17
-9
lines changed
  • docs/csharp/language-reference/compiler-messages

1 file changed

+17
-9
lines changed

docs/csharp/language-reference/compiler-messages/cs0433.md

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ The type TypeName1 exists in both TypeName2 and TypeName3
1414

1515
Two different assemblies referenced in your application contain the same namespace and type, which produces ambiguity.
1616

17-
To resolve this error, use the alias feature of the ([**References**](../compiler-options/inputs.md#references)) compiler option or do not reference one of your assemblies.
17+
To resolve this error, use the `extern alias` feature with project reference aliases or do not reference one of your assemblies. You can also use the alias feature of the ([**References**](../compiler-options/inputs.md#references)) compiler option when compiling directly with the C# compiler.
1818

1919
This error can also occur if:
2020

@@ -27,7 +27,7 @@ This error can also occur if:
2727

2828
```csharp
2929
// CS0433_1.cs in CS0433_1.csproj
30-
// or compile with: /target:library
30+
// compile with: dotnet build or /target:library
3131
namespace TypeBindConflicts
3232
{
3333
public class AggPubImpAggPubImp { }
@@ -38,14 +38,14 @@ namespace TypeBindConflicts
3838

3939
```csharp
4040
// CS0433_2.cs in CS0433_2.csproj
41-
// or compile with: /target:library
41+
// compile with: dotnet build or /target:library
4242
namespace TypeBindConflicts
4343
{
4444
public class AggPubImpAggPubImp { }
4545
}
4646
```
4747

48-
So, when consuming these two libraries (`CS0433_1.dll` and `CS0433_2.dll`) in the project, using the `AggPubImpAddPubImp` type will be ambiguous and will lead to compiler error `CS0433`.
48+
So, when consuming these two libraries (`CS0433_1.dll` and `CS0433_2.dll`) in the project, using the `AggPubImpAggPubImp` type will be ambiguous and will lead to compiler error `CS0433`.
4949

5050
```xml
5151
<!-- CS0433_3.csproj -->
@@ -55,7 +55,7 @@ namespace TypeBindConflicts
5555

5656
```csharp
5757
// CS0433_3.cs in CS0433_3.csproj
58-
// or compile with: /reference:cs0433_1.dll /reference:cs0433_2.dll
58+
// compile with: dotnet build or /reference:cs0433_1.dll /reference:cs0433_2.dll
5959
using TypeBindConflicts;
6060

6161
public class Test
@@ -67,7 +67,11 @@ public class Test
6767
}
6868
```
6969

70-
The following example shows how you can use the alias feature of the **/reference** compiler option or `<Aliases>` feature in `<ProjectReference>` to resolve this CS0433 error.
70+
The following example shows how you can use the `extern alias` feature with project references to resolve this CS0433 error. This is the recommended approach for modern .NET projects.
71+
72+
### Step 1: Add an alias to one of the project references
73+
74+
First, modify your project file to add an alias to one of the conflicting project references:
7175

7276
```xml
7377
<!-- CS0433_4.csproj -->
@@ -76,21 +80,25 @@ public class Test
7680
</ProjectReference>
7781
<ProjectReference Include="..\CS0433_2\CS0433_2.csproj" />
7882
```
83+
84+
### Step 2: Use the extern alias in your code
85+
86+
Then, use the `extern alias` directive and qualified type names to distinguish between the two types:
7987

8088
```csharp
8189
// CS0433_4.cs in CS0433_4.csproj
82-
// compile with: /reference:cs0433_1.dll /reference:CustomTypes=cs0433_2.dll
90+
// compile with: dotnet build or /reference:cs0433_1.dll /reference:CustomTypes=cs0433_2.dll
8391
extern alias CustomTypes;
8492
using TypeBindConflicts;
8593

8694
public class Test
8795
{
8896
public static void Main()
8997
{
90-
// AggPubImpAggPubImp taken from CS0433_1.dll
98+
// AggPubImpAggPubImp taken from CS0433_2.dll (no alias, default global namespace)
9199
AggPubImpAggPubImp n6 = new AggPubImpAggPubImp();
92100

93-
// AggPubImpAggPubImp taken from CS0433_2.dll
101+
// AggPubImpAggPubImp taken from CS0433_1.dll (via CustomTypes alias)
94102
CustomTypes.TypeBindConflicts.AggPubImpAggPubImp n7 =
95103
new CustomTypes.TypeBindConflicts.AggPubImpAggPubImp();
96104
}

0 commit comments

Comments
 (0)