Edit

Share via


Compiler Error CS0433

The type TypeName1 exists in both TypeName2 and TypeName3

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

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 option when compiling directly with the C# compiler.

This error can also occur if:

  • The @ Page directive has a CodeFile attribute when it should be a CodeBehind attribute.
  • Code is placed in an App_Code folder that shouldn't reside there.

How to identify the conflicting assemblies

The full error message shows which assemblies contain the conflicting type. The message format is:

error CS0433: The type 'N.C' exists in both 'A, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' and 'B, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'

In this example, the conflicting assemblies are A and B. The assembly names appear in single quotes before the version information. Use these assembly names to determine which references are causing the conflict and decide how to resolve it.

Examples

This code creates the DLL with the first copy of the ambiguous type.

// CS0433_1.cs in CS0433_1.csproj  
// compile with: dotnet build or /target:library  
namespace TypeBindConflicts
{  
   public class AggPubImpAggPubImp { }  
}  

This code creates the DLL with the second copy of the ambiguous type.

// CS0433_2.cs in CS0433_2.csproj  
// compile with: dotnet build or /target:library  
namespace TypeBindConflicts
{  
   public class AggPubImpAggPubImp { }  
}  

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.

<!-- CS0433_3.csproj -->
<ProjectReference Include="..\CS0433_1\CS0433_1.csproj" />  
<ProjectReference Include="..\CS0433_2\CS0433_2.csproj" />  
// CS0433_3.cs in CS0433_3.csproj  
// compile with: dotnet build or /reference:cs0433_1.dll /reference:cs0433_2.dll  
using TypeBindConflicts;

public class Test
{  
   public static void Main()
   {  
      AggPubImpAggPubImp n6 = new AggPubImpAggPubImp();   // CS0433  
   }  
}  

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.

Step 1: Add an alias to one of the project references

First, modify your project file to add an alias to one of the conflicting project references:

<!-- CS0433_4.csproj -->  
<ProjectReference Include="..\CS0433_1\CS0433_1.csproj">  
  <Aliases>CustomTypes</Aliases>
</ProjectReference>
<ProjectReference Include="..\CS0433_2\CS0433_2.csproj" />  

Step 2: Use the extern alias in your code

Then, use the extern alias directive and qualified type names to distinguish between the two types:

// CS0433_4.cs in CS0433_4.csproj  
// compile with: dotnet build or /reference:cs0433_1.dll /reference:CustomTypes=cs0433_2.dll  
extern alias CustomTypes;  
using TypeBindConflicts;  

public class Test
{  
   public static void Main()
   {  
      // AggPubImpAggPubImp taken from CS0433_2.dll (no alias, default global namespace)
      AggPubImpAggPubImp n6 = new AggPubImpAggPubImp();
      
      // AggPubImpAggPubImp taken from CS0433_1.dll (via CustomTypes alias)
      CustomTypes.TypeBindConflicts.AggPubImpAggPubImp n7 =
          new CustomTypes.TypeBindConflicts.AggPubImpAggPubImp();
   }  
}