Skip to content

Extern alias C# 2.0interop

Disambiguate types with the same fully-qualified name from different assemblies.

Occasionally two different assemblies define a type with the same fully-qualified name (same namespace and class name). In C# 1.0 there was no way to use both types in the same project - any reference to the name was ambiguous and the compiler reported error CS0433.

C# 2.0 introduces extern alias which lets you assign a named alias to an assembly reference. You can then use the alias as a root namespace to disambiguate between the two types.

Code ​

csharp
extern alias Vendor1;
extern alias Vendor2;

Vendor1::Acme.Logging.Logger v1Logger = new Vendor1::Acme.Logging.Logger();
Vendor2::Acme.Logging.Logger v2Logger = new Vendor2::Acme.Logging.Logger();
csharp
// Not possible to use both types - the name is ambiguous (CS0433)
// Must wrap one of the assemblies in a separate project/assembly
// that exposes it under a different name

Notes ​

  • The alias is assigned to the assembly reference in the project file (<Aliases>Vendor1</Aliases>) or via the /r compiler switch (e.g. /r:Vendor1=Vendor1.Logging.dll)
  • extern alias directives must appear at the top of the file, before any using directives
  • The global alias always refers to the default global namespace
  • This is rarely needed but essential when it is, particularly when working with multiple versions of the same library
  • See global using directives C# 10.0 for applying a using across a whole project, which combines with an extern alias.

More information ​

Powered by Intracia