You can use various types of aliases in C #. You can use a simple one:
using WF = System.Windows.Forms; //só fez o alias do namespace
Usage:
var botao = new WF.Button();
This can be most useful when there is ambiguity. Because deep down when there is not, that WF
would not even be necessary. So why create an alias to use in a place that would not even have to have anything? Some even like to be more explicit to avoid reading ambiguity. As there may be future ambiguity, the programmer already puts the full name there. As these names can be very long this way facilitates a little typing and reading space.
A better example might be in a place that needs to use a specific Stream
and in the same code use another that has the same elements (same types) with the same names. If you do not put the complete qualifier, the compiler refuses for obvious reasons. Putting the full qualifier is too long, you create a shorter alias.
The ideal is to use caution, you can not play with names by taste, it has to be a way to solve one problem, not cause another.
Or you can do something more complex:
using Dict = System.Collections.Generic.Dictionary<string, List<string>>;
Usage:
var dict = new Dict { "palavra", new List<string> { "definição1", "definicão2" }, "palavra2", new List<string> { "definição1", "definicão2" } };
Here it is more interesting because the alias is not of the namespace
but of the type, and a complex good that would be annoying to use several times in the code, in this way can refer to it in a simpler way without adding anything in the code, as with WF
above.
Some will say that this is more DRY , I disagree that this is exactly DRY, it just avoids repetition.
Another legal example of Marc Gravell's response .
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 .