Common Type System—Defining a Namespace


Jump to: navigation, search
CSharp-Online.NET:Articles
.NET Articles

Common Type System

© 2006 Wiley Publishing Inc.

Defining a Namespace

To place a type inside a namespace in C#, you simply wrap its declaration inside a namespace block:

namespace MyCompany.FooProject
{
   public class Foo { /*...*/ }
   public struct Bar { /*...*/ }
 
   namespace SubFeature
   {
      public class Baz { /*...*/ }
   }
}

Notice that we have a top-level namespace MyCompany.FooProject in which classes Foo and Bar reside. Their fully qualified names are MyCompany.FooProject.Foo and MyCompany.FooProject.Bar, respectively. We then have a nested namespace, named SubFeature. It’s not required to place it lexically within the enclosing namespace; we could have instead typed it out completely in the same or a separate file, that is, as MyCompany.FooProject.SubFeature. It contains a single type, Baz, whose fully qualified name is MyCompany.FooProject.SubFeature.Baz.


Previous_Page_.gif Next_Page_.gif


Personal tools