Namespaces can be nested : NameSpace « Language Basics « C# / C Sharp






Namespaces can be nested

Namespaces can be nested
/*
C#: The Complete Reference 
by Herbert Schildt 

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// 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"); 
      } 
    } 
  } 
} 
 
public class NestedNSDemo { 
  public static void Main() { 
    NS1.ClassA a= new NS1.ClassA(); 
 
 // NS2.ClassB b = new NS2.ClassB(); // Error!!! NS2 is not in view 
 
    NS1.NS2.ClassB b = new NS1.NS2.ClassB(); // this is right 
  } 
}

           
       








Related examples in the same category

1.Using the alias keyword to refer to a nested namespace
2.Define an alias to represent a namespace
3.Creating an alias
4.Namespaces are additiveNamespaces are additive
5.Illustrates the use of two namespacesIllustrates the use of two namespaces
6.The use of namespace hierarchies (part 1)The use of namespace hierarchies (part 1)
7.How the using statement is used to specify namespacesHow the using statement is used to specify namespaces
8.Using namespaceUsing namespace
9.Demonstrates using as a statementDemonstrates using as a statement
10.Demonstrate a namespaceDemonstrate a namespace
11.Namespaces prevent name conflictsNamespaces prevent name conflicts
12.Demonstrate a namespace 2Demonstrate a namespace 2
13.C# Namespaces and UsingC# Namespaces and Using