Breaking News

Top Step

Lesson 6: Namespaces

This lesson introduces you to C# Namespaces. Our objectives are as follows:

Understand what Namespace is.

Learn how to implement the using directive.

Learn to use alias directive.

Understand what are namespace members.

In Lesson 1, you saw the using System; directive in the SimpleHello program. This directive allowed you to use members of the Systemnamespace. Because of the narrow focus of that lesson, we needed to delay explanation until now. When you’ve completed this lesson you will understand the using directive and more.

Namespaces are C# program elements designed to help you organize your programs. They also provide assistance in avoiding name clashes between two sets of code. Implementing Namespaces in your own code is a good habit because it is likely to save you from problems later when you want to reuse some of your code. For example, if you created a class named Console, you would need to put it in your own namespace to ensure that there wasn’t any confusion about when the System.Console class should be used or when your class should be used. Generally, it would be a bad idea to create a class named Console, but in many cases your classes will be named the same as classes in either the .NET Framework Class Library or a third party library and namespaces help you avoid the problems that identical class names would cause.

Namespaces don’t correspond to file or directory names. If naming directories and files to correspond to namespaces helps you organize your code, then you may do so, but it is not required.

Listing 6-1 shows how to create a namespace. We declare the new namespace by putting the word namespace in front of csharp_station. Curly braces surround the members inside the csharp_stationnamespace.

Namespaces allow you to create a system to organize your code. A good way to organize your namespaces is via a hierarchical system. You put the more general names at the top of the hierarchy and get more specific as you go down. This hierarchical system can be represented by nested namespaces. Listing 6-2 shows how to create a nested namespace. By placing code in different sub-namespaces, you can keep your code organized.

Listing 6-3 shows another way of writing nested namespaces. It specifies the nested namespace with the dot operator betweencsharp_station and tutorial. The result is exactly the same as Listing 6-2. However, Listing 6-3 is easier to write.

Listing 6-4 provides an example of how to call namespace members with fully qualified names. A fully qualified name contains every language element from the namespace name down to the method call. At the top of the listing there is a nested namespacetutorial within the csharp-stationnamespace with classmyExample1 and method myPrint1. Main() calls this method with the fully qualified name oftutorial.myExample1.myPrint1(). Since Main() and the tutorialnamespace are located in the same namespace, using csharp_station in the fully qualified name is unnecessary.

At the bottom of Listing 6-4 is an addition to the csharp_station.tutorialnamespace. The classes myExample1 and myExample2 both belong to the same namespace. Additionally, they could be written in separate files and still belong to the same namespace. In Main(), themyPrint2() method is called with the fully qualified name tutorial.myExample2.myPrint2(). Although the class myExample2 is outside the bounding braces of where the method myPrint2 is called, the namespacecsharp_station does not need to be a part of the fully qualified name. This is because both classes belong to the same namespace, csharp_station.

Notice that I used different names for the two classes myExample1 and myExample2. This was necessary because every namespacemember of the same type must have a unique name. Remember, they are both in the same namespace and you wouldn’t want any ambiguity about which class to use. The methods myPrint1() and myPrint2() have different names only because it would make the lesson a little easier to follow. They could have had the same name with no effect, because their classes are different, thus avoiding any ambiguity.

If you would like to call methods without typing their fully qualified name, you can implement the using directive. In Listing 6-5, we show two using directives. The first, using System, is the same using directive you have seen in every program in this tutorial. It allows you to type the method names of members of the Systemnamespace without typing the word System every time. In myPrint(), Console is a class member of the Systemnamespace with the method WriteLine(). Its fully qualified name is System.Console.WriteLine(…).

Similarly, the using directive using csharp_station.tutorial allows us to call members of the csharp_station.tutorialnamespace without typing the fully qualified name. This is why we can type myExample.myPrint(). Without the using directive, we would have to typecsharp_station.tutorial.myExample.myPrint() every time we wanted to call that method.

Sometimes you may encounter a long namespace and wish to have it shorter. This could improve readability and still avoid name clashes with similarly named methods. Listing 6-6 shows how to create an alias with the alias directive using csTut = csharp_station.tutorial.myExample. Now the expression csTut can be used anywhere, in this file, in place ofcsharp_station.tutorial.myExample. We use it in Main().

Also in Main() is a call to the myPrint() method of the AliasDirectiveclass. This is the same name as the myPrint() method in themyExampleclass . The reason both of these methods can be called in the same method call is because the myPrint() method in themyExampleclass is qualified with the csTut alias. This lets the compiler know exactly which method is to be executed. Had we mistakenly omitted csTut from the method call, the compiler would have set up the myPrint() method of the AliasDirectiveclass to run twice.

So far, all we’ve shown in our namespaces are classes. However, namespaces can hold other types as follows:

Classes

Structures

Interfaces

Enumerations

Delegates

Future chapters will cover what these types are in more detail.

In summary, you know what a namespace is and you can declare your own namespaces. If you don’t want to type a fully qualified name, you know how to implement the using directive. When you want to shorten a long namespace declaration, you can use the alias directive. Also, you have been introduced to some of the other namespace members in addition to the class type.

I invite you to return for Lesson 7: Introduction to Classes.

Khabza Mkhize

Steps To Become the person you want to be in life is in your hands, Business, Education Money, Career