More About the using Directive

   


More About the using Directive

As you know, the using keyword, followed by a suitable namespace name, allows us to abbreviate a class's long fully-qualified name when we refer to it in the source code. Consequently, if we were to use the classes from the two namespaces Sams.CSharpPrimerPlus.ElevatorSimulation and Sams.CSharpPrimerPlus.BankSimulation (defined in Listing 15.3) in the source code of Figure 15.1, we could use the short classnames of the classes belonging to these namespaces by including lines 1 and 2 shown in Figure 15.1.

Figure 15.1. The compiler cannot know to which namespace Building belongs.
graphics/15fig01.gif

But what if both namespaces referenced by the using directive contain a class of the same name, as is the case with the Building class that is defined both in lines 22 38 of the Sams.CSharpPrimerPlus.ElevatorSimulation namespace and in lines 47 57 of the Sams.CSharpPrimerPlus.BankSimulation namespace in Listing 15.3? We would then have to specify the fully-qualified name of the Building class we wanted to use, despite the using directives in lines 1 and 2, to clear up this uncertain situation for the compiler. For example, if we wanted to specify the Sams.CSharpPrimerPlus.ElevatorSimulation.Building class, we would have to exchange Building in line 20 with this long name.

Class and Namespace Aliases

Apart from employing the using keyword to allow shortcuts when specifying classnames, you can also use it to declare aliases for classes (and other types like enums, structs, interfaces, and delegates) and namespaces. The syntax for performing this declaration is specified in Syntax Box 15.2. The alias is positioned after the using keyword, which is followed by the = symbol and the name of the namespace or type name you want to assign to this alias.

Syntax Box 15.2 Declaring Namespace and Type Aliases

 Namespace_alias_declaration::=             using <Namespace_alias> = <Namespace_name>; Type_alias_declaration::=             using <Type_alias> = <Type_name>; 

Note:

You can specify an alias for any of the following general types: classes, enums, interfaces, delegates, and structs.

The next example shows you how aliases are specified and why they can be convenient to use. In Figure 15.1 (shown earlier), we were, for ambiguity reasons, obliged to use the long name Sams.CSharpPrimerPlus.ElevatorSimulation.Building instead of just Building to specify this class, despite the using directive in line 1. Specifying this class many times in our code requires many keystrokes with an increased risk of typing errors and bulkier looking code. Line 1 of Figure 15.2 declares the leaner word ElevatorBuilding to be an alias for the awkward classname Sams.CSharpPrimerPlus.ElevatorSimulation.Building and allows us to use this shorter name in line 20 and anywhere else in the code affected by line 1.

Figure 15.2. Class alias and namespace alias.
graphics/15fig02.gif

Line 2 further illustrates how we can specify a namespace alias. In this case, ElevatorSimulation is stated to be an alias for the namespace Sams.CSharpPrimerPlus.ElevatorSimulation. This permits us to specify the Person class from the Sams.CSharpPrimerPlus.ElevatorSimulation namespace by writing ElevatorSimulation.Person in line 21 and declare a new variable of this type. In this case, the variable is arbitrarily called pedestrian.

Avoid Specifying Aliases for the Types of the .NET Framework Class Library

graphics/tnt.gif

Most programmers are familiar with the names of the .NET Framework classes, so creating new aliases for these names can make the code less self-documenting. For example if you saw the following in another programmer's code

 TxtInOut.Write("What does this mean?"); 

you would probably be confused about its meaning until you saw the following alias specification in the beginning of the code:

 using TxtInOut = System.Console; 


Do Not Over-Abbreviate the Alias Names

graphics/bulb.gif

Choose meaningful alias names in your source code to improve clarity and readability.

For example, if, instead of the alias ElevatorBuilding specified in the following line of Figure 15.5

Figure 15.5. Distributing a large ElevatorSimulation.exe to all our clients.
graphics/15fig05.gif
 using ElevatorBuilding = Sams.CSharpPrimerPlus.ElevatorSimulation.Building; 

we had used the alias ElBld as follows

 using ElBld = Sams.CSharpPrimerPlus.ElevatorSimulation.Building; 

we would have saved some typing, but we would have made the code more cryptic.



   


C# Primer Plus
C Primer Plus (5th Edition)
ISBN: 0672326965
EAN: 2147483647
Year: 2000
Pages: 286
Authors: Stephen Prata

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net