What use is the Aliases property of assembly references in Visual Studio 8

C#Visual StudioReferenceCsproj

C# Problem Overview


When I add an assembly reference to a project in Visual Studio 8 the Aliases property, of that reference, is set to "global". What is this property good for and why is it set to global?

MSDN tells me that this is a list of aliases for the assembly but not why I might want to use this property or why most are aliased as "global".

MSDN reference

C# Solutions


Solution 1 - C#

This is for "extern aliases". Suppose you want to use two different types, both of which are called Foo.Bar (i.e. Bar in a namespace of Foo). The two types will be in different assemblies (by definition) - you use the property in VS to associate an alias with each reference, then you can do:

extern alias FirstAlias;
extern alias SecondAlias;

using FirstBar = FirstAlias::Foo.Bar;
using SecondBar = SecondAlias::Foo.Bar;

and then use FirstBar and SecondBar in your code.

So basically it's an extra level of naming - and you shouldn't use it unless you really, really have to. It will confuse a lot of people. Try to avoid getting into that situation in the first place - but be aware of this solution for those times where you just can't avoid it.

Solution 2 - C#

Search for "extern alias"; it is a very rarely used feature that is only needed to disambiguate between two dlls that contribute the same types (for example, two different versions of the same assembly, or two assemblies that have a class which shares a fully-qualified-name).

"global" is the default. For example, if you have a class called Foo.System, you can unambiguously refer to the main System namespace via global::System.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSpencer BoothView Question on Stackoverflow
Solution 1 - C#Jon SkeetView Answer on Stackoverflow
Solution 2 - C#Marc GravellView Answer on Stackoverflow