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 reference both types in the same file - one would always shadow the other.

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

C#
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();
C#
// Not possible to reference both types - one will always shadow the other
// Must restructure code to avoid using both assemblies in the same file

Notes

  • The alias is assigned to the assembly reference in the project file or via the /r compiler switch (e.g. /r:Vendor1=Vendor1.Logging.dll)
  • 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

More information