It is possible to use several types of aliases in C#. You can use a simple:
using WF = System.Windows.Forms; //só fez o alias do namespace
Use:
var botao = new WF.Button();
This can be more useful when there is ambiguity. Because deep down when there is, this WF
nor would be necessary. So why create an alias to use in a place that would not need to have anything?
Some even like to be more explicit to avoid reading ambiguity, but being explicit can leave the code long. As there may be future ambiguity, the programmer already puts the full name there. As these names can be very long this way makes typing and reading space a little easier.
A better example might be in a place that needs to use one Stream
and in the same code use another one that has the same elements (same types) with the same names. If you do not complete the qualifier, the compiler refuses for obvious reasons. Putting the full qualifier gets too long, you create a shorter alias.
The ideal is to use with caution, can not keep playing with the names by taste, has to be a way to solve a problem, not cause another.
Or you can do something more complex:
using Dict = System.Collections.Generic.Dictionary<string, List<string>>;
Use:
var dict = new Dict { "palavra", new List<string> { "definição1", "definicão2" }, "palavra2", new List<string> { "definição1", "definicão2" } };
I put in the Github for future reference.
Here is more interesting because the alias is not from namespace
and yes of the type, and a very complex that would be boring to use several times in the code, this way can refer to it more simply without adding anything in the code, as happened with the WF
above.
Some will say that this is more like DRY, I disagree that this is exactly DRY, just avoid repetition.
Another legal example of Reply by Marc Gravell.
namespace RealCode {
//using Foo; // can't use this - it breaks DoSomething
using Handy = Foo.Handy;
using Bar;
static class Program {
static void Main() {
Handy h = new Handy(); // prove available
string test = "abc";
test.DoSomething(); // prove available
}
}
}
namespace Foo {
static class TypeOne {
public static void DoSomething(this string value) { }
}
class Handy {}
}
namespace Bar {
static class TypeTwo {
public static void DoSomething(this string value) { }
}
}
Documentation.
Quick C# galley to answer :)
– Wallace Maxters
The initial idea was to "use it to decrease typing," but I know this is lazy.
– Wallace Maxters
As I said in reply, to namespaces in fact occurs the opposite, in most cases not making alias is the normal and shortest form.
– Maniero