Csharp/CSharp Tutorial/Language Basics/Namespace

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Creates a namespace with a single class

using System;
namespace StringSwitch
{
  class MySwitch 
  {
  }
}
////////////////////////////////////////////
using System;
using StringSwitch;
class MainClass
{
  public static void Main() 
  {
    string localString;
    MySwitch s = new MySwitch();
  }
}

Declare a namespace

namespace Counter { 
  class MyClass { 
  } 
}

Demonstrate a namespace.

using System; 
 
namespace Counter { 
  class MyClass { 
  } 
} 
 
class MainClass { 
  public static void Main() { 
    Counter.MyClass my = new Counter.MyClass(); 
  } 
}

Demonstrate the :: qualifier.

using System;  
  
using Counter; 
using AnotherCounter; 
 
// Give Counter an alias called Ctr. 
using Ctr = Counter;  
  
namespace Counter {  
  class MyClass { 
  }  
}  
 
namespace AnotherCounter {  
  class MyClass { 
  } 
} 
 
class MainClass {  
  public static void Main() {  
 
    Ctr::MyClass m = new Ctr::MyClass();  
  }  
}

Full qualify name

For example, the line


Console.WriteLine("A simple C# program.");

Namespace Introduction

  1. The namespace provides a way to keep one set of names separate from another.
  2. Names declared in one namespace will not conflict with the same names declared in another.
  3. The namespace System is reserved for items in the .NET Framework class library.
  4. The using keyword simply states that the program is using the names in the given namespace.

1.5.Namespace 1.5.1. Namespace Introduction 1.5.2. <A href="/Tutorial/CSharp/0020__Language-Basics/Fullqualifyname.htm">Full qualify name</a> 1.5.3. <A href="/Tutorial/CSharp/0020__Language-Basics/Simplestnamespacewithclassdefinition.htm">Simplest namespace with class definition</a> 1.5.4. <A href="/Tutorial/CSharp/0020__Language-Basics/Declareanamespace.htm">Declare a namespace</a> 1.5.5. <A href="/Tutorial/CSharp/0020__Language-Basics/Demonstrateanamespace.htm">Demonstrate a namespace.</a> 1.5.6. <A href="/Tutorial/CSharp/0020__Language-Basics/Usefullyqualifiedclassname.htm">Use fully qualified class name</a> 1.5.7. <A href="/Tutorial/CSharp/0020__Language-Basics/TwolevelsofNamespaces.htm">Two levels of Namespaces</a> 1.5.8. <A href="/Tutorial/CSharp/0020__Language-Basics/Namespacewithdot.htm">Namespace with dot</a> 1.5.9. <A href="/Tutorial/CSharp/0020__Language-Basics/Renamethenamespaces.htm">Rename the namespaces</a> 1.5.10. <A href="/Tutorial/CSharp/0020__Language-Basics/Namespacespreventnameconflicts.htm">Namespaces prevent name conflicts</a> 1.5.11. <A href="/Tutorial/CSharp/0020__Language-Basics/Namespacesareadditive.htm">Namespaces are additive.</a> 1.5.12. <A href="/Tutorial/CSharp/0020__Language-Basics/Namespacescanbenested.htm">Namespaces can be nested</a> 1.5.13. <A href="/Tutorial/CSharp/0020__Language-Basics/Demonstratethequalifier.htm">Demonstrate the :: qualifier.</a> 1.5.14. <A href="/Tutorial/CSharp/0020__Language-Basics/Theuseoftwonamespaces.htm">The use of two namespaces</a> 1.5.15. <A href="/Tutorial/CSharp/0020__Language-Basics/Theuseofnamespacehierarchies.htm">The use of namespace hierarchies</a> 1.5.16. <A href="/Tutorial/CSharp/0020__Language-Basics/Createsanamespacewithasingleclass.htm">Creates a namespace with a single class</a> 1.5.17. <A href="/Tutorial/CSharp/0020__Language-Basics/SameclassnameunderdifferentNamespaces.htm">Same class name under different Namespaces</a>

Namespaces are additive.

using System; 
 
using Counter; 
 
namespace Counter { 
  class MyClass { 
  } 
} 
 
namespace Counter { 
  class MySecondClass { 
  } 
} 
 
class MainClass { 
  public static void Main() { 
    MyClass m = new MyClass(); 
    MySecondClass cu = new MySecondClass(); 
  } 
}

Namespaces can be nested

using System; 
 
namespace NS1 { 
  class ClassA { 
     public ClassA() { 
       Console.WriteLine("constructing ClassA"); 
    } 
  } 
  namespace NS2 { // a nested namespace 
    class ClassB { 
       public ClassB() { 
         Console.WriteLine("constructing ClassB"); 
      } 
    } 
  } 
} 
 
class MainClass { 
  public static void Main() { 
    NS1.ClassA a = new NS1.ClassA(); 
 
    NS1.NS2.ClassB b = new NS1.NS2.ClassB();
  } 
}
constructing ClassA
constructing ClassB

Namespaces prevent name conflicts

using System;  
  
namespace Counter {  
  class MyClass {  
    public MyClass(){
      Console.WriteLine("Counter1 namespace."); 
    }
  }  
}  
 
namespace Counter2 {  
  class MyClass { 
    public MyClass() { 
      Console.WriteLine("Counter2 namespace."); 
    } 
  } 
} 
 
class MainClass {  
  public static void Main() {  
    // This is CountDown in the Counter namespace. 
    Counter.MyClass m1 = new Counter.MyClass();  
 
    // This is CountDown in the default namespace. 
    Counter2.MyClass m2 = new Counter2.MyClass(); 
 
  }  
}
Counter1 namespace.
Counter2 namespace.

Namespace with dot

namespace Outer.Inner
{
    class MyClass
    {
        public static void Function() {}
    }
}

Rename the namespaces

using ThatConsoleClass = System.Console;
class MainClass
{
    public static void Main()
    {
        ThatConsoleClass.WriteLine("Hello");
    }
}
Hello

Same class name under different Namespaces

using System;     
namespace Foo {
  public class Money {
    public Money() {
    }
    public void Print( ) {
      Console.WriteLine("Foo.Money.Print");
    }
  }
}
namespace Bar {
  public class Money {
    public Money( ) {
    }
    public void Print( ) {
      Console.WriteLine("Bar.Money.Print");
    }
  }
}

public class MainClass {
  public static void Main( ) {
    Foo.Money fm = new Foo.Money();
    Bar.Money bm = new Bar.Money();
    fm.Print( );
    bm.Print( );
  }
}
Foo.Money.Print
Bar.Money.Print

Simplest namespace with class definition

A namespace is declared using the namespace keyword.

The general form of namespace is shown here:


namespace name {
    // members
    }

The use of namespace hierarchies

namespace CompanyName
{
  namespace UserInterface  // nested namespace
  {
    public class MyClass
    {
      public void Test()
      {
        System.Console.WriteLine("UserInterface Test()");
      }
    }
  }
}

namespace CompanyName.DatabaseAccess  // nested namespace using dot
{
  public class MyClass
  {
    public void Test()
    {
      System.Console.WriteLine("DatabaseAccess Test()");
    }
  }
}

class MainClass {
  public static void Main() {
    CompanyName.UserInterface.MyClass myUI = new CompanyName.UserInterface.MyClass();
    CompanyName.DatabaseAccess.MyClass myDB = new CompanyName.DatabaseAccess.MyClass();
    CompanyName.DatabaseAccess.MyClass myMT = new CompanyName.DatabaseAccess.MyClass();
    myUI.Test();
    myDB.Test();
    myMT.Test();
  }
}
UserInterface Test()
DatabaseAccess Test()
DatabaseAccess Test()

The use of two namespaces

namespace CompanyName
{
  public class Car
  {
    public string make;
  }
}
namespace DifferentCompany
{
  public class Car
  {
    public string make;
  }
}
class MainClass
{
  public static void Main()
  {
    System.Console.WriteLine("Creating a CompanyName.Car object");
    CompanyName.Car myCar = new CompanyName.Car();
    myCar.make = "Toyota";
    System.Console.WriteLine("myCar.make = " + myCar.make);
    System.Console.WriteLine("Creating a DifferentCompany.Car object");
    DifferentCompany.Car myOtherCar = new DifferentCompany.Car();
    myOtherCar.make = "Porsche";
    System.Console.WriteLine("myOtherCar.make = " + myOtherCar.make);
  }
}
Creating a CompanyName.Car object
myCar.make = Toyota
Creating a DifferentCompany.Car object
myOtherCar.make = Porsche

Two levels of Namespaces

namespace Outer
{
    namespace Inner
    {
        class MyClass
        {
            public static void Function() {}
        }
    }
}

Use fully qualified class name

class Example { 
 
  public static void Main() { 
 
    // Console.WriteLine is fully qualified. 
    System.Console.WriteLine("A simple C# program."); 
  } 
}
A simple C# program.