What is the benefit of using namespace aliases? Is it good only for simplify of coding?
Advantages of namespace In one program, namespace can help define different scopes to provide scope to different identifiers declared within them. By using namespace - the same variable names may be reused in a different program.
The namespace keyword is used to declare a scope that contains a set of related objects. You can use a namespace to organize code elements and to create globally unique types.
Namespace Alias Qualifier(::) makes the use of alias name in place of longer namespace and it provides a way to avoid ambiguous definitions of the classes. It is always positioned between two identifiers. The qualifier looks like two colons(::) with an alias name and the class name. It can be global.
The biggest advantage of using namespace is that the class names which are declared in one namespace will not clash with the same class names declared in another namespace. It is also referred as named group of classes having common features.
I use namespace aliases only if i have conflicts with classes. for me it's not simplifing at all. My opinion: if there is no need, don't use it.
As everyone else has said, it is useful for disambiguating types when you have to import multiple namespaces at the same time.
This seems to be against other people's opinions, but it may also be useful for clarifying scope between two domains:
using Gilco.Test.ProjectX;
using DevCode = Gilco.Nilfum.Alpha.ProjectX;
public class MyTests
{
public void TestCase1()
{
var car = new DevCode.Car();
var testCar = new TestCar();
}
}
It may also be useful in the rare case when you have to specify the namespace (for disambiguation), your namespace isn't so long to justify the alias, but is likely to change soon (like when you're using namespaces to support two versions of your code path simultaneously):
using DevCode = Gilco.V1;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With