Namespace in C#
Namespace in C#
property.propertyNumber = "D294FF";
property.Price = 425880;
}
}
Application: Accessing Members of a Namespace
1. To access the content of a namespace, change the document as follows:
2. public class DepartmentStore
3. {
4. static int Main()
5. {
6. Store.StoreItem si = new Store.StoreItem();
7.
8. si.itemNumber = 613508;
9. si.itemName = "Merino Crew Neck Cardigan";
10. si.unitPrice = 80.00M;
11.
12. System.Console.WriteLine("Store Inventory");
13. System.Console.Write("Item #: ");
14. System.Console.WriteLine(si.itemNumber);
15. System.Console.Write("Item Name: ");
16. System.Console.WriteLine(si.itemName);
17. System.Console.Write("Unit Price: ");
18. System.Console.WriteLine(si.unitPrice);
19.
20. System.Console.ReadKey();
21. return 0;
22. }
23. }
24.
25. namespace Store
26. {
27. public class StoreItem
28. {
29. public int itemNumber;
30. public string itemName;
31. public decimal unitPrice;
32. }
}
33. Execute the application to see the result:
34. Store Inventory
35. Item #: 613508
36. Item Name: Merino Crew Neck Cardigan
Unit Price: 80.00
37. Press Enter and return to your programming environment
namespace Dealership
{
public class Car
{
public decimal price;
}
}
}
property.propertyNumber = "D294FF";
property.price = 425880;
}
}
In the same way, you can nest as many namespaces inside of other namespaces as you
judge necessary.
Another technique used to nest a namespace consists of starting to create one. Then,
after its name, type a period followed by a name for the nested namespace. Here is an
example:
namespace Geometry.Quadrilaterals
{
}
After creating the nested namespace, you can access its contents by qualifying it. Here
is an example:
namespace Geometry.Quadrilaterals
{
public class Square
{
public double side;
}
}
sqr.side = 25.85;
}
}
In the same way, you can nest other namespaces inside of one. Here are examples:
namespace Geometry.Quadrilaterals
{
namespace Geometry.Rounds
{
}
In the same way, you can create as many namespaces as necessary inside of others.
After nesting a namespace, to access its content, you can qualify the desired name.
Here is an example:
namespace Geometry.Quadrilaterals
{
public class Square
{
public double side;
}
}
namespace Geometry.Volumes.Elliptic
{
public class Cylinder
{
public double radius;
}
}
sqr.side = 25.85;
cyl.radius = 36.85;
}
}
Application: Nesting Namespaces
1. Click the Records.cs label to access its document and change it as follows:
2. namespace Store
3. {
4. namespace Inventory
5. {
6. public class StoreItem
7. {
8. public int itemNumber;
9. public string itemName;
10. public decimal unitPrice;
11. }
12. }
13.
14. namespace Personel
15. {
16. namespace PayrollRecords
17. {
18. public class Employee
19. {
20. public string firstName;
21. public string lastName;
22. public decimal HourlySalary;
23. }
24.
25. public class Contractors
26. {
27. public string FullName;
28. public int ContractStatus;
29. }
30. }
31. }
}
32. Access the DepartmentStore.cs file and change it as follows:
33. using Supply;
34. using Store.Inventory;
35.
36. public class DepartmentStore
37. {
38. static int Main()
39. {
40. . . . No Change
41.
42. return 0;
43. }
}
Class Overloading? No Way! Way
Unlike the methods of a class, you cannot overload the name of a class in a file. That is,
you cannot have two classes with the same name in the same scope. One alternative is
to put each class in its own namespace. Here is an example:
namespace Arithmetic
{
public class Numbers
{
public int value;
}
}
namespace Algebra
{
public class Numbers
{
public int value;
}
}
Another alternative will be available when we study generics. That is, in the same
namespace, one of the classes can be a generic and the other not. Here is an example:
namespace Arithmetic
{
public class Numbers
{
public int value;
}
Managing Namespaces
Inserting a Namespace
If you have existing code already, you can include it in a namespace. To do that:
Click above the section of code, type namespace followed by a name and {. Locate the end of
the section and type }
Select the code that you want to include in a namespace. Right-click the selection and click
Surround With... In the list, double-click namespace
Renaming a Namespace
Renaming a namespace follows the same logic we reviewed for variables, classes, and
methods: you can manually rename it or benefit from the assistance of the Code Editor.
To rename a namespace:
In the Code Editor, locate its name and change it. Then click the arrow of its tag and choose
an option from the menu
Right-click the name and click Rename... Then follow the dialog boxes as we have reviewed
already
1.
Namespaces and Inheritance
Imagine you had created a class named Person in a namespace named People as
follows:
using System;
namespace People
{
public class Person
{
private string _name;
private string _gdr;
namespace HighSchool
{
public class Teacher : People.Person
{
private string _pos;
class Exercise
{
public static int Main()
{
People.Person man = new People.Person("Herman Sandt", "Male");
HighSchool.Teacher staff = new HighSchool.Teacher("Vice Principal");
Console.WriteLine();
return 0;
}
}
Alternatively, to use the contents of a namespace, prior to calling a member of that
namespace, you can type the using keyword followed by the name of the namespace.
Here is an example:
Source File: Exercise.cs
using System;
using People;
using HighSchool;
class Exercise
{
public static int Main()
{
Person man = new Person("Herman Sandt", "Male");
Teacher staff = new Teacher("Vice Principal");
Console.WriteLine();
return 0;
}
}
Application: Using Inheritance With Namespaces
1. To create a new application, on the main menu, click File -> New Project...
2. In the middle list, click Empty Project
3. Set the Name to Geometry3 and press Enter
4. To create a new class, on the main menu, click Project -> Add Class...
5. Set the Name to Square and press Enter
6. Change the file as follows:
7. using System;
8. using System.Collections.Generic;
9. using System.Linq;
10. using System.Text;
11.
12. namespace Geometry3
13. {
14. public class Square
15. {
16. private double sd;
17.
18. public Square(double s = 0.00D)
19. {
20. this.sd = s;
21. }
22. }
}
23. To create a new class, on the main menu, click Project -> Add Class...
24. Set the Name to Rectangle and press Enter
25. Change the file as follows:
26. using System;
27. using System.Collections.Generic;
28. using System.Linq;
29. using System.Text;
30.
31. namespace Geometry3
32. {
33. public class Rectangle
34. {
35. double len;
36. double hgt;
37.
38. public Rectangle(double L = 0.00D, double H = 0.00D)
39. {
40. this.len = L;
41. this.hgt = H;
42. }
43. }
}
return 0;
}
}
You can also precede System with global and the :: operator. Here is an example:
public class Exercise
{
static int Main()
{
global::System.Console.WriteLine("The wonderful world of C#
programming");
return 0;
}
}
We learned that, to access a namespace, you can use the using keyword. In the same
way, you can use using to refer to the System namespace. Here is an example:
using System;
return 0;
}
}
This time too, you can precede System with global::
using global::System;
return 0;
}
}
Once you have used using System or using global::System, you can use or
omit System in the body of the function. Here is an example:
using System;
return 0;
}
}
Introduction to Other Namespaces
The .NET Framework provides an incredibly long list of namespaces. We cannot review
all of them now. Eventually, when we have to use a class, we will indicate in which
namespace it exists.
As mentioned already, we saw in Lesson 5 that we could create a new class using the
Add New Class option from the main menu, the Solution Explorer, or the Class View. If
you use one of those techniques, the studio will also add other namespaces. For now,
we will accept that code "as is".
.NET Data Types
All of the data types we have used so far are represented by classes in the .NET
Framework. 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:
C# Data Type Equivalent .NET Class C# Data Type Equivalent .NET Class
bool Boolean char Char
byte Byte sbyte SByte
short Int16 ushort UInt16
int Int32 uint UInt32
long Int64 ulong UInt64
float Single double Double
decimal Decimal
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 those classes,
type its name. Here is an example:
class Operations
{
public double Addition()
{
Double a;
Double b;
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 of them has is called ToString. As its name implies, it is
used to convert a value to a string.