Skip to main content

When Two Referenced Assemblies Have Overlapping Namespace

You will come across a problem at some point, where you will use a class, that exists in two libraries that your project references.

This problem is identified by the CS0433 compiler error, “The type exists in both…”.

To workaround this, without updating one of the assemblies, which might not be possible if they are third-party, you can use an alias.

NOTE: This will work with raw assemblies and nuget packages.

To do so, right-click the assembly and give it an Alias, like this:

image.png

The alias string must not contain periods, or you will get a syntax error during usage.

Then, place an extern alias statement for the alias at very top of each source file where you are using the class (whose name exists more than once). Like this:

image.png

This extern alias now shifts the aliased assembly into the alternate name, so you can distinguish them and pick the class from the appropriate library.

You can now reference classes in the aliased assembly, like this:

var fff = new blissogacommon.oga.Common.SpecialTypes.Serialization_Helper();

Or, like this:

var fff = new blissogacommon::oga.Common.SpecialTypes.Serialization_Helper();

And, still have access to the other class in the non-aliased namespace, like this:

var fff = new oga.Common.SpecialTypes.Serialization_Helper();