Aliases: overcoming name conflicts
What is it?
Aliases provide a means of overcoming conflicts between namespaces and, for example, classes that use the same names.
Which C# version supports it?
All versions of C# support aliases.
Why use it?
When more than one person is involved in developing software, there is a risk of name conflicts. Rules on using verbose names that describe the task in hand well and using reversed URLs as namespace names can help minimise such conflicts. However even the best rules get broken sometimes, or simply fail to cater for some obscure edge-case.
How to use it
Global Alias
Imagine my colleague and I want to write a meaning of life program. He will write part of it; I’ll write the other part. So off she goes and writes:
namespace AnswerToAll
{
public class MeaningOfLife
{
public static int FortyTwo = 42;
}
}
Meanwhile, I write my part:
namespace Arno
{
class AnswerToAll
{
int answer = AnswerToAll.MeaningOfLife.FortyTwo;
}
}
Since we are both addressing the issue of an answer to all, we have both using the same name: AnswerToAll. Unfortunately, she used it as a namespace and I used it as a class name. If you try to compile this code, it gives an error:
error CS0117: 'Arno.AnswerToAll' does not contain a
definition for 'MeaningOfLife'
The compiler has to deal with scope and so, within the scope of Arno.AnswerToAll, the AnswerToAll namespace is invisible.
Luckily this is easily fixed by changing my code to:
namespace Arno
{
class AnswerToAll
{
int answer = global::AnswerToAll.MeaningOfLife.FortyTwo;
}
}
Now it compiles just fine. The “trick” here is the use of the “global” alias. This is a predefined alias that effectively points to the base of the namespace tree. That tree looks like the following in our case:
global -- AnswerToAll - MeaningOfLife
'- Arno - AnswerToAll
So when we qualify AnswerToAll.MeaningOfLife with the global alias, it knows which AnswerToAll we mean and correctly compiles.
User-defined Aliases
Global isn’t the only alias available: we can define our own too. This can act as syntactic sugar in helping to simplify expressions when conflicts arise. So I could rewrite my code to:
using meaning = AnswerToAll.MeaningOfLife;
namespace Arno
{
class AnswerToAll
{
int answer = meaning.FortyTwo;
}
}
Here I have defined the alias “meaning”, which refers to the class AnswerToAll.MeaningOfLife. An important note here is that I’ve written meaning.FortyTwo, rather than meaning::FortyTwo. The “::” notation is used when the alias refers to a namespace; the dot notation is used when it refers to a class (or anything else inside a namespace).
No comments yet. Be the first.
Leave a reply
