Namespaces
In this part of the C# tutorial, we will describe namespaces.Namespaces are used to organize code at the highest logical level. They classify and present programming elements that are exposed to other programs and applications. Within a namespace, we can declare another namespace, a class, an interface, a struct, an enum or a delegate. We cannot define items such as properties, variables and events. These items must be declared within containers such as structures or classes. Namespaces prevent ambiguity and simplify references when using large groups of objects such as class libraries.
Namespaces organize objects in an assembly. An assembly is a reusable, versionable and self-describing building block of a CLR application. Assemblies can contain multiple namespaces. Namespaces can contain other namespaces. An assembly provides a fundamental unit of physical code grouping. A namespace provides a fundamental unit of logical code grouping.
public class CSharpAppThe built-in libraries are organized within namespaces. Take the Console class. It is available within the System namespace. To call the static WriteLine() method of the Console class, we use its fully qualified name. Fully qualified names are object references that are prefixed with the name of the namespace where the object is defined.
{
static void Main()
{
System.Console.WriteLine("Simple namespace example");
}
}
In the following code, we have two files that share the same namespace.
using System;We have a ZetCode namespace. In the namespace, we have a class Example.
// namespace2.cs
namespace ZetCode
{
public class Example
{
public int x = 0;
public void Raise()
{
x += 100;
Console.WriteLine(x);
}
}
}
namespace ZetCodeWe declare a namespace called ZetCode. The code goes inside the curly brackets of the ZetCode namespace.
{
...
}
// namespace1.csIn the second file, we work with the Example class from the previous file. We invoke its Raise() method
namespace ZetCode
{
public class CSharpApp
{
static void Main()
{
Example ex = new Example();
ex.Raise();
ex.Raise();
}
}
}
namespace ZetCodeWe work in the same namespace.
Example ex = new Example();We create the instance of the Example class. We call its Raise() method twice. Because we work with objects of the same namespace, we do not need to specify its name.
ex.Raise();
ex.Raise();
$ gmcs namespace1.cs namespace2.csOutput.
$ ./namespace1.exe
100
200
The following code example has two distinct namespaces. We use the
using
keyword to import elements from a different namespace. // distinctnamespace2.csWe have a skeleton of a Math class in a MyMath namespace. In the Basic class, we define a PI constant and a GetPi() method.
namespace MyMath
{
public class Basic
{
public static double PI = 3.141592653589;
public static double GetPi()
{
return PI;
}
}
}
// distinctnamespace1.csIn this file, we use the elements from the MyMath namespace.
using MyMath;
using System;
namespace ZetCode
{
public class CSharpApp
{
static void Main()
{
Console.WriteLine(Basic.PI);
Console.WriteLine(Basic.GetPi());
}
}
}
using MyMath;We import the elements from the MyMath namespace into our namespace.
Console.WriteLine(Basic.PI)Now we can use those elements. In our case it is the Basic class.
Console.WriteLine(Basic.GetPI())
$ gmcs distinctnamespace1.cs distinctnamespace2.csWe compile the two files and run the program.
$ ./distinctnamespace1.exe
3.141592653589
3.141592653589
This part of the C# tutorial was dedicated to namespaces.
0 comments:
Post a Comment