articles

Home / DeveloperSection / Articles / Namespace

Namespace

Niraj Kumar Mishra 1928 25-Aug-2017
Need for Namespaces:

Namespaces allow us to create a system to organize your code. You can create namespaces via a hierarchical is good way to organize your code. You can also create a nested namespace by placing code in different sub-namespaces, you can keep your code organized .

Example:
namespace xyz.csharp.namespaces
{
    public class Demo
    {
        public string Message()
        {
            return "Hello, world";
        }
    }
}

Namespaces are hierarchical, and the name xyz.csharp.namespaces is actually shorthand for defining a namespace you can also make that namespace in this order:

namespace xyz
{
 namespace csharp
  {
    namespace namespaces
    {
      public string Message()  
      {
        return "Hello, world";
      }
    }
  }
}
Using alias directives

You can also use using directive as alias .by using alias you don’t need write full

path of namespace in program.

using identifier = namespace-or-type-name ;

Example:
namespace Namespace1.Namespace2
{
    class abc { }
}
namespace Namespace3
{
    using A = Namespace1.Namespace2;
    class xyz : abc { }
}

Here, within member declarations in the Namespace3 namespace, A is an alias for

Namespace1.Namespace2.

Using namespace directives

A using namespace directive imports the types contained in a namespace into the immediately enclosing compilation unit or namespace body, enabling the identifier of each type to be used without qualification .

using-namespace-directive:using namespace-name ;
Example:
namespace Namespace1.Namespace2 { class abc { } } namespace Namespace3 { using Namespace1.Namespace2; class xyz : abc { } }

Here, within member declarations in the Namespace3 namespace, the type

members of Namespace1.Namespace2 are directly available, and thus class

Namespace3.B derives from class Namespace1.Namespace2.abc



Updated 19-Aug-2019

Leave Comment

Comments

Liked By