Aliasing class names with the using keyword

Writing out fully qualified class names that include namespace identifiers can get a bit tedious, especially if the names are long. You can use the using keyword to provide an aliased name for the fully qualified class identifier, and you can use the alias name instead of the fully qualified class identifier once the alias is established.

You can alias a name with a statement having the following structure:

• The using keyword

• The fully qualified class name with the namespace identifier

• A statement terminating semicolon

Listing 12-2 adds to Listing 12-1 by aliasing the class names to shorter equivalents. The shorter names are then used by the Main() method to work with objects of the classes.

Listing 12-2: Aliasing Class Names using Class1 = Namespacei.TestClass; using Class2 = Namespace2.TestClass;

namespace Namespacel {

class TestClass {

public TestClass() {

System.Console.WriteLine("Hello from

Namespacel.TestClass!"); }

namespace Namespace2 {

class TestClass {

public TestClass() {

System.Console.WriteLine("Hello from

Namespace2.TestClass!"); }

class MainClass {

public static void Main() {

Classl Objectl = new Class1(); Class2 Object2 = new Class2();

As with the previous example, Listing 12-2 outputs the same messages you have previously seen. You can see these results in Figure 12-2.

IMhl LiNjir Its>

Figure 12-2: Aliasing class names

The using statements must appear in the source code before the namespaces themselves are declared. If the using statements appear after namespace declarations, you receive the following error message from the C# compiler:

error CS1529: A using clause must precede all other namespace elements

In Chapter 7, you see that the C# keywords defining variable types are actually structures defined by the .NET Framework. Take another look at Table 7-1 and notice the following:

• The value type structures reside in the .NET System namespace.

• The using keyword is used to alias the .NET structure names to the equivalent C# keywords. You may imagine Table 7-1 being implemented within the .NET Framework using C# statements such as the following:

• using short = System.Int16; // ... more declarations ...

You can alias namespace names as well as classes, as shown in Listing 12-3. Listing 12-3: Aliasing Namespace Names using N1 = Namespacel; using N2 = Namespace2;

namespace Namespace1 {

class TestClass {

public TestClass() {

System.Console.WriteLine("Hello from

Namespace1.TestClass!"); }

namespace Namespace2 {

class TestClass {

public TestClass() {

System.Console.WriteLine("Hello from

Namespace2.TestClass!"); }

class MainClass {

public static void Main() {

N1.TestClass Object1 = new N1.TestClass(); N2.TestClass Object2 = new N2.TestClass();

Declaring namespace directives with the using keyword

If you use a class declared in a namespace, you must prefix the class name with the namespace name, even if you aren't working with any other namespaces that may have a class with the same name. This is why the examples used until now have always called WriteLine() with the System namespace qualifier:

System.Console.WriteLine("Hello from C#!");

By default, forgetting to use the namespace name causes the C# compiler to issue an error:

error CS0234: The type or namespace name 'Console' does not exist in the class or namespace 'Namespace1'

Prefixing every class name with namespace names like System gets tedious, especially if you need to do it many times. Fortunately, you can use the using keyword to help reduce your coding time.

Using the using keyword with a namespace name tells the C# compiler that you want to refer to classes in the named namespace without prefixing the class names with the namespace name. For example, take a look at the following statement:

using System;

This is called a namespace directive. Namespace directives tell the C# compiler that the code will be using classes from the namespace and that the classes won't be prefixed with the namespace name. The C# compiler does the work of finding the definition of each class in one of the namespaces referenced in a namespace directive.

Listing 12-4 is a modification of Listing 12-2; it includes a using statement that references the .NET System namespace.

Listing 12-4: Using a Namespace Directive using System;

using Class1 = Namespace1.TestClass; using Class2 = Namespace2.TestClass;

namespace Namespace1 {

class TestClass

public TestClass() {

Console.WriteLine("Hello from Namespace1.TestClass!");

namespace Namespace2 {

class TestClass

public TestClass() {

Console.WriteLine("Hello from Namespace2.TestClass!");

class MainClass {

public static void Main() {

Classl Objectl = new Class1(); Class2 Object2 = new Class2();

The System namespace directive in Listing 12-4 enables the code to reference the Console class without prefixing it with the System namespace.

0 0

Post a comment