Click to Rate and Give Feedback
MSDN
MSDN Library
Visual Studio 2008
Visual Studio
Visual C#
C# Reference
C# Keywords
 extern alias

  Switch on low bandwidth view
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
C# Language Reference
extern alias (C# Reference)

You might have to reference two versions of assemblies that have the same fully-qualified type names. For example, you might have to use two or more versions of an assembly in the same application. By using an external assembly alias, the namespaces from each assembly can be wrapped inside root-level namespaces named by the alias, which enables them to be used in the same file.

NoteNote:

The extern keyword is also used as a method modifier, declaring a method written in unmanaged code.

To reference two assemblies with the same fully-qualified type names, an alias must be specified at a command prompt, as follows:

/r:GridV1=grid.dll

/r:GridV2=grid20.dll

This creates the external aliases GridV1 and GridV2. To use these aliases from within a program, reference them by using the extern keyword. For example:

extern alias GridV1;

extern alias GridV2;

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.

In the previous example, GridV1::Grid would be the grid control from grid.dll, and GridV2::Grid would be the grid control from grid20.dll.

For more information, see the following sections in the C# Language Specification:

  • 9.3 Extern aliases

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Example of how this works.      Todd King   |   Edit   |   Show History

This example is a little contrived but it demonstrates how this works:
A.cs:

namespace RefTest
{
    public class ClassA
    {
    }
}

>csc /target:library A.cs
>csc /target:library /out:A2.dll A.cs

Now I have A.dll and A2.dll

If I want to compile the following with a reference to both A.dll and A2.dll:
Bad.cs:

using System;
namespace RefTest
{
public class Program
{
public static int Main(string[] args)
{
ClassA a = new ClassA();
Console.WriteLine("ClassA = " + a);
return 0;
}
}
}

>csc /target:exe /reference:A.dll /reference:A2.dll Bad.cs
I'll get the following compiler error:
error CS0433: The type 'RefTest.ClassA' exists in both 'e:\NameConflictRefTest\A.dll' and 'e:\NameConflictRefTest\A2.dll'

To fix this I need to do the following:
Good.cs:

extern alias A2;
using System;
namespace RefTest
{
public class Program
{
public static int Main(string[] args)
{
ClassA a = new ClassA();
A2::RefTest.ClassA a2 = new A2::RefTest.ClassA();
Console.WriteLine("ClassA = " + a);
Console.WriteLine("A2::RefTest.ClassA = " + a2);
return 0;
}
}
}

>csc /target:exe /reference:A.dll /reference:A2=A2.dll Good.cs

Now I have Good.exe which references both A.dll and A2.dll and uses the ClassA from each of these two assemblies.

Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker