Lesson 06
Lesson 06
Lesson 06
LESSON 6 - NAMESPACES
B E G I N N E R S ’ S
C# TUTORIAL
6 . N A M E S P A C E S
WWW.CSHARP–STATION.COM
PAGE 1 OF 7
COPYRIGHT © 2000–2003 C# STATION , ALL RIGHTS RESERVED
BEGINNERS C# TUTORIAL
LESSON 6 - NAMESPACES
// Namespace Declaration
using System;
PAGE 2 OF 7
COPYRIGHT © 2000–2003 C# STATION , ALL RIGHTS RESERVED
BEGINNERS C# TUTORIAL
LESSON 6 - NAMESPACES
// Namespace Declaration
using System;
// Namespace Declaration
using System;
PAGE 3 OF 7
COPYRIGHT © 2000–2003 C# STATION , ALL RIGHTS RESERVED
BEGINNERS C# TUTORIAL
LESSON 6 - NAMESPACES
// Namespace Declaration
using System;
namespace csharp_station
{
// nested namespace
namespace tutorial
{
class myExample1
{
public static void myPrint1()
{
Console.WriteLine("First Example of calling another namespace member.");
}
}
}
PAGE 4 OF 7
COPYRIGHT © 2000–2003 C# STATION , ALL RIGHTS RESERVED
BEGINNERS C# TUTORIAL
LESSON 6 - NAMESPACES
Notice that I used different names for the two classes myExample1 and
myExample2. This was necessary because every namespace member 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.
// Namespace Declaration
using System;
using csharp_station.tutorial;
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 System namespace without typing the word
System every time. In myPrint(), Console is a class member of the System
PAGE 5 OF 7
COPYRIGHT © 2000–2003 C# STATION , ALL RIGHTS RESERVED
BEGINNERS C# TUTORIAL
LESSON 6 - NAMESPACES
// Namespace Declaration
using System;
using csTut = csharp_station.tutorial.myExample; // alias
PAGE 6 OF 7
COPYRIGHT © 2000–2003 C# STATION , ALL RIGHTS RESERVED
BEGINNERS C# TUTORIAL
LESSON 6 - NAMESPACES
class . The reason both of these methods can be called in the same method
call is because the myPrint() method in the myExample class 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 AliasDirective
class to run twice.
Classes
Structures
Interfaces
Enumerations
Delegates
Future chapters we 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.
PAGE 7 OF 7
COPYRIGHT © 2000–2003 C# STATION , ALL RIGHTS RESERVED