Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the benefit of using namespace aliases in C#? [duplicate]

Tags:

namespaces

c#

What is the benefit of using namespace aliases? Is it good only for simplify of coding?

like image 755
masoud ramezani Avatar asked Feb 24 '10 10:02

masoud ramezani


People also ask

What are the benefits of using namespaces?

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.

Why do we use namespace in C?

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.

What is namespace aliases in C#?

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.

What are the benefits of using namespaces in C#?

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.


2 Answers

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.

like image 161
gsharp Avatar answered Oct 12 '22 11:10

gsharp


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;
like image 36
Merlyn Morgan-Graham Avatar answered Oct 12 '22 10:10

Merlyn Morgan-Graham