Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Variable using declaration, namespace alias, using directive [duplicate]

Tags:

c#

.net

Basically my question is about the following:

using myName = System.Web;

I recently came across some code which did something very similar to that above.
My question boils down to the following points:

  1. What is doing this even called?
  2. Why would you need to this?
  3. Pros/Cons of doing this?

I understand one of the benefits in which I have used this type of declaration:

using Security = System.Web.Security;

...

_roles = (SimpleRoleProvider)Security.Roles.Provider;
_membership = (SimpleMembershipProvider)Security.Membership.Provider;

As I had an entity named Membership I couldn't just do Membership.Provider without a conflict. So here the using declaration allows me to shortern the full use of System.Web.Security.Membership to just Security.Membership.Provider which feels nicer from an OCD point of view.

like image 785
Ashley Medway Avatar asked Mar 09 '26 14:03

Ashley Medway


1 Answers

This is a using directive also called an namespace alias.

If you have two types of the same name but from different namespaces, you need to spell out the namespace every time you use that type. If it's a long namespace, you may want to give it an alias.

like image 145
nvoigt Avatar answered Mar 12 '26 03:03

nvoigt