Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What purpose does extern alias serve in C#?

To resolve a namespace conflict between assemblies in C#, I gave one an Alias of NLog:

enter image description here

Now, I can refer to a namespace within that assembly like so:

public class Logger : NLog::NLog.LogReceiverService
{
   // ...
}

However, this won't compile until I use the extern alias keyword at the top of my file:

extern alias NLog;

My Question:

What purpose does the extern alias keyword serve? Shouldn't NLog::NLog.LogReceiverService be sufficient to fully disambiguate the assembly alias, namespace and type I'm referring to?

like image 329
Mike Christensen Avatar asked Oct 01 '22 15:10

Mike Christensen


People also ask

What is extern alias?

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.

What is extern alias C#?

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.

Which of the following keyword is used to reference two assemblies with the same fully qualified type names?

Question 3: Which of the following keyword is used to reference two assemblies with the same fully qualified type names? This creates the external aliases GridV1 and GridV2 . To use these aliases from within a program, reference them by using the extern keyword.


1 Answers

Setting the Aliases property on the reference will only designate it as that alias while the initial load of assembly references is done for your project as run time. In order to use that aliased assembly reference in your file, you must specify that you are going to use it via the extern alias keyword, which sets the compiler directives to look for that alias.

You would think that they would have some type of global aliasing, but I am thinking they force the explicit alias use due to a performance hit that is incurred on the alias.

Here is the reference material on extern alias: http://msdn.microsoft.com/en-us/library/ms173212.aspx

like image 53
CoderCamps.com Avatar answered Oct 04 '22 18:10

CoderCamps.com