C# and Code Organization: Namespaces |
|
Introduction |
A namespace is a section of code that is identified with a specific name. The name could be anything such as somebody's name, the name of the company's department, or a city. To create a namespace, you start with the namespace keyword followed by the name of the section. Like a class, the section that is part of a namespace starts with an opening curly bracket "{" and ends with a closing curly bracket "}". Here is an example: namespace Business { } |
Between the curly brackets, you can type anything that is part of the namespace. For example, you can create a class inside of a namespace. Here is an example: namespace Business { class House { } }
After creating the necessary members of a namespace, you can use the period operator to access an item that is part of the namespace. To do this, in the desired location, type the name of the namespace, followed by a period, followed by the desired member of the namespace. Here is an example: using System; namespace Business { public class House { public string PropertyNumber; public decimal Price; } } class Program { static void Main() { Business.House property = new Business.House(); property.PropertyNumber = "D294FF"; property.Price = 425880; Console.WriteLine("=//= Altair Realty =//="); Console.WriteLine("Properties Inventory"); Console.Write("Property #: "); Console.WriteLine(property.PropertyNumber); Console.Write("Market Value: "); Console.WriteLine(property.Price); Console.WriteLine(); } } This would produce: =//= Altair Realty =//= Properties Inventory Property #: D294FF Market Value: 425880 Press any key to continue . . . You can create one namespace inside of another namespace. Creating a namespace inside of another is referred to as nesting the namespace. The namespace inside of another namespace is nested. To create a namespace inside of another, simply type it as you would create another namespace. Here is an example: namespace Business { public class House { public string PropertyNumber; public decimal Price; } namespace Dealer { } } In the example above, the Dealer namespace is nested inside of the Business namespace. After creating the desired namespaces, nested or not, you can create the necessary class(es) inside of the desired namespace. To access anything that is included in a nested namespace, you use the period operator before calling a member of a namespace or before calling the next nested namespace. Here is an example: using System; namespace Business { public class House { public string PropertyNumber; public decimal Price; } namespace Dealer { public class Car { public decimal Price; } } } class Program { static void Main() { Business.House property = new Business.House(); property.PropertyNumber = "D294FF"; property.Price = 425880; Console.WriteLine("=//= Altair Realty =//="); Console.WriteLine("Properties Inventory"); Console.Write("Property #: "); Console.WriteLine(property.PropertyNumber); Console.Write("Market Value: "); Console.WriteLine(property.Price); Console.WriteLine(); Business.Dealer.Car vehicle = new Business.Dealer.Car(); vehicle.Price = 38425.50M; Console.Write("Car Value: "); Console.WriteLine(vehicle.Price); } } This would produce: =//= Altair Realty =//= Properties Inventory Property #: D294FF Market Value: 425880 Car Value: 38425.50 Press any key to continue . . . In the same way, you can nest as many namespaces inside of other namespaces as you judge necessary.
To make programming in C# a little easier, many classes ship with it and they are created in various namespaces of the .NET Framework. Each namespace in C# is used to provide a specific set of classes. The most regularly used namespace in the C# language is called System. Inside of the System namespace is a class called Console. The Console class is used to display things on the console screen also called the DOS window. The Console class contains static methods to display information on the screen or to retrieve information from the user who types it in the DOS window. The method that is used to display text on the screen is called Write. To use the Write() method, inside of the parentheses, type the sentence between double-quotes. Here is an example: class Program { static void Main() { System.Console.Write("The Wonderful World of C# Programming"); } } Besides the Write() method, the Console class also provides another static method named WriteLine(). The difference is that, after displaying something on the screen, the Write() method keeps the caret on the same line but WriteLine() transfers the caret to the next line. We will see various examples of this behavior throughout our lessons. We saw that, to call an object or a method that is part of a namespace, you must "qualify" that method or object using the period operator. Instead of using this approach, if you already know the name of a namespace that exists or has been created in another file, you can use a special keyword to indicate that you are using a namespace that is defined somewhere. This is done with the using keyword. To do this, on top of the file (preferably), type using followed by the name of the namespace. With the using keyword, you can include as many external namespaces as necessary.
In C# (unlike many other languages such as C/C++, Pascal, etc), all of the data types we have used so far are in fact complete classes. This means that they are equipped with methods. These classes are defined in the System namespace. The classes of these data types are defined as:
This means that, if you don't want to use the data types we have reviewed so far, you can use the class that is defined in the System namespace. To use one of these types, type the System namespace followed by a period. Then type the equivalent class you want to use. Here is an example: class Operations { public double Addition() { System.Double a; System.Double b; System.Double c; a = 128.76; b = 5044.52; c = a + b; return c; } } Because the regular names of data types we introduced in the previous lessons are more known and familiar, we will mostly use them. Because the data types are defined as classes, they are equipped with methods. One of the methods that each one them has is called ToString. As it s name implies, it is used to convert a value to a string. |
|
||
Previous | Copyright © 2006-2016, FunctionX, Inc. | Next |
|