It's dangerous to code alone! Take this.

Extern Aliases

NOTE: This article was once planned for the content of The C# Player’s Guide but was deemed too advanced to spend the page count on it. I am including it here, rather than trashing it. Fair warning: it is an advanced feature that you will probably never actually use.

On rare occasion, you may need to reference two versions of the same assembly or dependency. This results in two sets of types and namespaces that have a significant amount of overlap. It is difficult to distinguish one from the other. This is extremely rare, and you should go to great lengths to avoid this. But when it is unavoidable, the way to deal with it is with an extern alias.

An extern alias allows you to bring in the code in a DLL under a separate “root” namespace, parallel to the global namespace, with a different name of your own choosing.

You must do two things to use an extern alias. First, apply a namespace alias to the DLL that you’re referencing. This is not hard to do:

  1. In the Solution Explorer, in your project, under the References element, find the assembly that you want to give an alias to.
  2. Right-click on it and select Properties.
  3. Look for the property called Aliases. By default, this will say “global”. Simply change this to another value. For example, “MyAlias”.

Then in code files that need to use things from this DLL, you make the alias available with a line like the following at the start of the file:

extern alias MyAlias;

Note that extern aliases must go before anything else in the file, including using directives.

At this point, everything in your new aliased namespace is accessible like you’re used to, with your alias prepended to the front. So you can add using directives for it, reference types in it with fully qualified names, etc. Additionally, you can access stuff in this namespace using the namespace alias operator (::) as well: MyAlias::My.Namespace.MyClass. This helps illustrate that your extern alias is not under the global namespace but is at the root level beside it.

You should avoid having to do this whenever possible, but it is nice to know the option is there when things get weird.