Home SEO Tools HTML Escape Tool Character Counter Guest post

Namespaces in CSharp

0 comments


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 CSharpApp
{
static void Main()
{
System.Console.WriteLine("Simple namespace example");
}
}
The 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.

In the following code, we have two files that share the same namespace.
using System;

// namespace2.cs

namespace ZetCode
{
public class Example
{
public int x = 0;

public void Raise()
{
x += 100;
Console.WriteLine(x);
}
}
}
We have a ZetCode namespace. In the namespace, we have a class Example.
namespace ZetCode
{
...
}
We declare a namespace called ZetCode. The code goes inside the curly brackets of the ZetCode namespace.
// namespace1.cs

namespace ZetCode
{
public class CSharpApp
{
static void Main()
{
Example ex = new Example();
ex.Raise();
ex.Raise();
}
}
}
In the second file, we work with the Example class from the previous file. We invoke its Raise() method
namespace ZetCode
We work in the same namespace.
Example ex = new Example();
ex.Raise();
ex.Raise();
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.
$ gmcs namespace1.cs namespace2.cs
$ ./namespace1.exe
100
200
Output.

The following code example has two distinct namespaces. We use the using keyword to import elements from a different namespace.
// distinctnamespace2.cs

namespace MyMath
{
public class Basic
{
public static double PI = 3.141592653589;

public static double GetPi()
{
return PI;
}
}
}
We have a skeleton of a Math class in a MyMath namespace. In the Basic class, we define a PI constant and a GetPi() method.
// distinctnamespace1.cs

using MyMath;
using System;

namespace ZetCode
{
public class CSharpApp
{
static void Main()
{
Console.WriteLine(Basic.PI);
Console.WriteLine(Basic.GetPi());
}
}
}
In this file, we use the elements from the MyMath namespace.
using MyMath;
We import the elements from the MyMath namespace into our namespace.
Console.WriteLine(Basic.PI)
Console.WriteLine(Basic.GetPI())
Now we can use those elements. In our case it is the Basic class.
$ gmcs distinctnamespace1.cs distinctnamespace2.cs
$ ./distinctnamespace1.exe
3.141592653589
3.141592653589
We compile the two files and run the program.
This part of the C# tutorial was dedicated to namespaces.

Do you like this Tutorial? Please link back to this article by copying one of the codes below.

URL: HTML link code: BB (forum) link code:

No comments:

FAQs | Privacy Policy | Contact | | Advertise | Donate