09 - C# Namespaces

9.1 Introduction to Namespace

Namespaces are elements used for organizing the programs in an application. The name clashes occurring in the code is avoided due to use of namespaces. The code can be easily reused by implementing the namespaces. They do not correspond to any file or directory.

The syntax to define the namespace is as shown below:

namespace namespace1
{
//code to be used
}

To call the namespace version, function or variable add the namespace name as follows:

namespace_name.item_name;

The code to demonstrate the namespace is as shown below:

namespace Csharp_tutorial
{
    namespace demo
    {
        class Program
        {
            static void Main( string[ ] args )
            {
                Console.WriteLine(“This is demo for C# tutorial”);
            }
        }
    }
}

In the code shown above, we declare a namespace by adding the namespace word before Csharp_tutorial. We have added curly brackets after the namespace and before class declaration.

The code for calling the members of the namespace is as shown below:

namespace Csharp_tutorial
{
    namespace demo
    {
        class Program
        {
            public static void Show()
            {
                Console.WriteLine(“Calling the Show method in C#”);
            }
        }
        class data
        {
            static void Main ( string[ ] args )
            {
                demo.Program.Show(); 
                Console.Read();
            }
        }
    }
}

The output for the code is as shown below:

In the code the namespace members are fully qualified names. A fully qualified name contains language element from the namespace name to the method call. The first namespace is the Csharp_tutorial. It contains class program and Show method defined in it. The Main() method calls the fully qualified name as demo.Program.Show.

Nested namespaces in C#

When a namespace is declared inside another namespace, the declaration is called as nested namespace. The nesting can be performed at any level user wants to perform.

There are two ways to demonstrate the nested namespace in C#.

1) Using fully qualified name of the class

using System;
namespace name1
{
    namespace name2
    {
        namespace name3
        {
            class Program
            {
                //class code
            }
        }
    }
}

2) Using the dot operator in the namespace declaration

using System;
namepsace name1.name2.name3
{
//code to be executed
}

The code sample to demonstrate the nested namespace in C# is as shown below:

using System.Text;
using firstname;
using firstname.lastname;
namespace firstname
{
    class days1
    {
        public void days()
        {
            Console.WriteLine(“The day is Sunday”);
        }
    }
    namespace lastname
    {
        class month
        {
            public void months()
            {
                Console.WriteLine(“The month is May”);
            }
        }
    }
    class Program
    {
        static void Main ( string[ ] args )
        {
            days1 d = new days1();
            month m = new month();
            d.days();
            m.months();
            Console.Read();
        }
    }
}

 

The output for the code is as shown below:

9.2 Implement namespace the using directive

When user wants to call the methods without typing the fully qualified name, user can implement the using directive. The using directive imports the types contained in the namespace to the immediately enclosed unit. It enables the identifier of each type to be used without qualification.

The syntax to declare the namespace through using directive is as shown below:

namespace names1.names2
{
Class A{ }
}
namespace names3
{
using names1.names2;
Class B:A{ }
}

The type members of names1 and names2 are directly available. The class names3.B derives from class names1.names2.A.

The code snippet to demonstrate the using directive in namespace is as shown below:

using name1;
using name2;

namespace name1
{
    class add
    {
        public void display()
        {
            Console.WriteLine(“The function is to display the addition”);
        }
    }
}
namespace name2
{
    class divide
    {
        public void Show()
        {
            Console.WriteLine(“The function is to display the division”);
        }
    }
}

class Program
{
    static void Main ( string [ ] args )
    {
        add a = new add();
        divide d = new divide();
        a.display();
        d.Show();
        Console.Read();
    }
}

The output for the code is as shown below:

9.3 Use the alias directive

A using alias directive is an identifier that provides as an alias for a namespace. They are used for creating new type names. They are used to point existing type and namespace. The general syntax to create alias directive is as shown below:

using alias-directive:
using identifier = namespace or type name;

A sample code for the use of alias directive is as shown below;

using System.Text;
using name = System.Text.StringBuilder;
namespace ConsoleApplication10
{
    class Program
    {
        static void Main ( string[ ] args )
        {
            name n = new name();
            n.Append(“John”);
            n.Append(“Mischa”);
            Console.WriteLine(n);    
            Console.Read();
        }
    }
}

The output for the code is as shown below:

9.4 Members of the namespace

Namespaces have members associated with it. The namespaces and types declared within a namespace are members of the namespace. They do not have restrictions. The names are not declared as private, protected or internal namespaces. They are always accessed publicly.

There are several members in the namespace of C#. The list of the members is as shown below:

1) Struct members: The members of a struct are the members declared in the struct and the members inherited from class object. The simple type members correspond to the struct type members.  

2) Enumeration members: The constants declared in the enumeration are the enumeration members. The members of enumeration are inherited from class System.Enum.

3) Class members: The members declared in the class are known as class members and the inherited base classes. The members inherited from base class can be constants, methods, fields, events, properties, operators of the base class.

A class member can be declaration of constants, fields, methods, properties, events, indexers, operators, constructors and destructors.

The members of the object and string are related to the members of class types as follows:

a) The members of the string are present in the System.String class

b) The members of the object are present in the System.Object class

4) Interface members: The members declared in the interface are known as interface members. The members are inherited from the class object

5) Array members: The array members are inherited from System.Array class

6) Delegate members: The members declared in the delegate are inherited from System.Delegate class

Like us on Facebook