Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 261
Spencer Booth Avatar asked Nov 13 '08 09:11

Spencer Booth


People also ask

How do you use assembly references?

In the Project Designer, click the References tab. Click the Add button to open the Add Reference dialog box. In the Add Reference dialog box, select the tab indicating the type of component you want to reference. Select the components you want to reference, and then click OK.

What is extern alias in C#?

Each extern alias declaration introduces an additional root-level namespace that parallels (but does not lie within) the global namespace. Thus types from each assembly can be referred to without ambiguity by using their fully qualified name, rooted in the appropriate namespace-alias.


1 Answers

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.

like image 94
Jon Skeet Avatar answered Oct 19 '22 11:10

Jon Skeet