namespace in c#

By | February 13, 2019

Namespace -: Namespaces provide a way to group related top-level members into a hierarchy. They are also used to avoid naming conflicts. A top-level member, such as a class, that is not included in a namespace is said to belong to the default namespace. It can be moved to another namespace by being enclosed in a namespace block. The naming convention for namespaces is the same as for classes, with each word initially capitalized.

namespace MyNamespace
{
  class MyClass {}
}

Types of Namespace – There are following types

Nested Namespaces-: Namespaces can be nested any number of levels deep to further define the namespace hierarchy.as like

namespace MyNamespace
{
  namespace NestedNamespace
  {
    class MyClass {}
  }
}

Another type way to write this is to separate the namespaces with a dot.

namespace MyNamespace.NestedNamespace
{
  class MyClass {}
}

In the declaring the same namespace again in another class within the project has the same effect as if both namespaces were included in the same block, even if the class is located in another source code file.

Namespace Access-: To access a class from another namespace, we need to specify its fully qualified name.

namespace MyNamespace.NestedNamespace
{
  public class MyClass {}
}
namespace OtherNamespace
{
  class MyApp
  {
    static void Main()
    {
      MyNamespace.NestedNamespace.MyClass myClass;
    } 
  }
}

Using Directive-: The fully qualified name can be shortened by including the namespace with a using directive. The members of that namespace can then be accessed anywhere in the code file without having to pre-opened the namespace to every reference. It is mandatory to place using directives before all other members in the code file.

using MyNamespace.NestedNamespace;

Having direct access to these members means that if there is a conflicting member signature in the current namespace, the member in the included namespace will be hidden.

For example, if there is a MyClass in the OtherNamespace as well, that class will be used by default. To use the class in the included namespace, the fully qualified name would again have to be specified.

using MyNamespace.NestedNamespace;
namespace MyNamespace.NestedNamespace
{
  public class MyClass
  {
    public static int x;
  }
}
namespace OtherNamespace
{
  public class MyClass
  {
    static void Main()
    {
      int x = MyNamespace.NestedNamespace.MyClass.x;
    }
  }
}

To simplify this reference, the using directive can instead be changed to assign the namespace to an alias.

using MyAlias = MyNamespace.NestedNamespace;
// ...
int x = MyAlias.MyClass.x;

An even shorter way would be to define the fully qualified class name as a new type for the code file, by using the same alias notation.

using MyType = MyNamespace.NestedNamespace.MyClass;
// ...
int x = MyType.x;

A using static directive was added in C# 6. This directive imports only the accessible static members of the type into the current namespace. In the following example, static members of the Match class can be used without qualification due to the using static directive.

using static System.Math;
public class Circle
{
  public double radius { get; set; }
  public double Area
  {
    get { return PI * Pow(radius, 2); }
  }
}

 

Write a programme using name space-:

using System;
using first_space;
using second_space;

namespace first_space {
   class abc {
      public void func() {
         Console.WriteLine("welcome in netnic first page");
      }
   }
}
namespace second_space {
   class efg {
      public void func() {
         Console.WriteLine("This is a computer and internet related");
      }
   }
}   
class TestClass {
   static void Main(string[] args) {
      abc fc = new abc();
      efg sc = new efg();
      fc.func();
      sc.func();
      Console.ReadKey();
   }
}

The Output of this Programme -:

welcome in netnic first page
This is a computer and internet related

 

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *