C # Tutorial C # namespace (Namespace)

Source: Internet
Author: User


C # Namespace (Namespace)


The design purpose of the namespace is to provide a way to separate a group of names from other names. The names of classes declared in one namespace do not conflict with the names of the same classes declared in another namespace.


Define namespace


The definition of a namespace begins with the keyword namespace, followed by the name of the namespace, as follows:


namespace namespace_name

{

   // code declaration

}

In order to call a function or variable that supports a namespace version, the name of the namespace is placed in front, as shown below:


namespace_name.item_name;

The following program demonstrates the use of namespaces:


using System;

namespace first_space

{

   class namespace_cl

   {

      public void func ()

      {

         Console.WriteLine ("Inside first_space");

      }

   }

}

namespace second_space

{

   class namespace_cl

   {

      public void func ()

      {

         Console.WriteLine ("Inside second_space");

      }

   }

}

class TestClass

{

   static void Main (string [] args)

   {

      first_space.namespace_cl fc = new first_space.namespace_cl ();

      second_space.namespace_cl sc = new second_space.namespace_cl ();

      fc.func ();

      sc.func ();

      Console.ReadKey ();

   }

}

When the above code is compiled and executed, it produces the following results:


Inside first_space

Inside second_space

using keyword


The using keyword indicates that the program uses the name in the given namespace. For example, we use the System namespace in the program, which defines the class Console. We can just write:


Console.WriteLine ("Hello there");

We can write the fully qualified name as follows:


System.Console.WriteLine ("Hello there");

You can also use the using namespace directive so that you do n’t need to prefix the namespace name when using it. This directive tells the compiler that subsequent code uses the names in the specified namespace. The following code delays the application of the namespace.


Let's use the using specification to rewrite the above example:


using System;

using first_space;

using second_space;


namespace first_space

{

   class abc

   {

      public void func ()

      {

         Console.WriteLine ("Inside first_space");

      }

   }

}

namespace second_space

{

   class efg

   {

      public void func ()

      {

         Console.WriteLine ("Inside second_space");

      }

   }

}

class TestClass

{

   static void Main (string [] args)

   {

      abc fc = new abc ();

      efg sc = new efg ();

      fc.func ();

      sc.func ();

      Console.ReadKey ();

   }

}

When the above code is compiled and executed, it produces the following results:


Inside first_space

Inside second_space

Nested namespace


Namespaces can be nested, that is, you can define another namespace within one namespace, as follows:


namespace namespace_name1

{

   // code declaration

   namespace namespace_name2

   {

     // code declaration

   }

}

You can use the dot (.) Operator to access the members of the nested namespace as follows:


using System;

using first_space;

using first_space.second_space;


namespace first_space

{

   class abc

   {

      public void func ()

      {

         Console.WriteLine ("Inside first_space");

      }

   }

   namespace second_space

   {

      class efg

      {

         public void func ()

         {

            Console.WriteLine ("Inside second_space");

         }

      }

   }

}

 

class TestClass

{

   static void Main (string [] args)

   {

      abc fc = new abc ();

      efg sc = new efg ();

      fc.func ();

      sc.func ();

      Console.ReadKey ();

   }

}

When the above code is compiled and executed, it produces the following results:


Inside first_space

Inside second_space

The above is [c # tutorial] C # Namespace (Namespace) content, more related content please pay attention to topic.alibabacloud.com (www.php.cn)!


  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.