Using Namespaces…………

Let’s examine some code that uses the SomeUtility class I defined in the previous section:

public class EntryPoint

{

static void Main()

{

Acme.Utilities.SomeUtility util =

new Acme.Utilities.SomeUtility();

}

}

This practice of always qualifying names fully is rather verbose and might eventually lead to a bad case of carpal tunnel syndrome. The using namespace directive avoids this. It tells the compiler that you’re using an entire namespace in a compilation unit or another namespace. What the using keyword does is effectively import all of the names in the given namespace into the enclosing namespace, which could be the global namespace of the compilation unit. The following example demonstrates this:

using Acme.Utilities;

public class EntryPoint

{

static void Main()

{

SomeUtility util = new SomeUtility();

}

}

The code is now much easier to deal with and somewhat easier to read. The using directive, because it is at the global namespace level, imports the type names from Acme.Utilities into the global namespace. Sometimes when you import the names from multiple namespaces, you may still have naming conflicts if both imported namespaces contain types with the same name. In this case, you can import individual types from a namespace, creating a naming alias. This technique is available via namespace aliasing in C#. Let’s modify the usage of the SomeUtility class so that you alias only the SomeUtility class rather than everything inside the Acme.Utilities namespace:

namespace Acme.Utilities

{

class AnotherUtility() {}

}

using SomeUtility = Acme.Utilities.SomeUtility;

public class EntryPoint

{

static void Main()

{

SomeUtility util = new SomeUtility();

Acme.Utilities.AnotherUtility =

new Acme.Utilities.AnotherUtility();

}

}

In this code, the identifier SomeUtility is aliased as Acme.Utilities.SomeUtility. To prove the point, I augmented the Acme.Utilities namespace and added a new class named AnotherUtility. This class must be fully qualified in order for you to reference it, since no alias is declared for it. Incidentally, it’s perfectly valid to give the previous alias a different name than SomeUtility. Although giving the alias a different name may be useful when trying to resolve a naming conflict, it’s generally better to alias it using the same name as the original class name in order to avoid maintenance confusion in the future.

SHARE THE KNOWLEDGE


  

Advertisements

About Dinesh

I am engineering student........ I am selected in Microsoft Student Partner as MSP............

Posted on November 10, 2011, in C#, Knowledge, Language. Bookmark the permalink. 4 Comments.

  1. ya offcource u find more info on this site……….

  1. Pingback: E-bike

  2. Pingback: egrccuelr

  3. Pingback: home remedies for warts

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: