Grouping Classes into Namespaces


It is very likely for two different banking companies to have a class called Checking. If both companies then shared their code with other developers, it would be difficult for the developers to distinguish between the two classes. For that reason, Microsoft groups their classes in units called namespaces. They recommend that developers also do the same.

A namespace is a language feature that appends a prefix to every class name to make the class name unique. The resulting class name will be the name of the namespace, a dot, and the name of the class.

To add a namespace:

  1. Add the word namespace in lower case followed by the name of the namespace, followed by an open curly bracket .

  2. Place one or more class definitions in the namespace.

  3. End the namespace declaration with a closing curly bracket ( Figure 2.58 ).

    Figure 2.58 Namespaces can be used to group classes by company, and by functionality.
      namespace Banking {  public class Checking    {      //code omitted for simplicity    }    public class Savings    {      //code omitted for simplicity    } } 

graphics/tick.gif Tips

  • When you create a project with Visual Studio, the wizard uses the project name as the default namespace. That means that every time you add a new file to your project, the wizard will add the declaration of the namespace to the file ( Figure 2.59 ). You can change the name of the default namespace by choosing Project > Properties, then entering or deleting the default namespace in the general properties ( Figure 2.60 ).

    Figure 2.59. Whenever you generate a new class file, or Web Form in your project, the wizard adds a default namespace declaration with the name of the project and puts the new code inside it.

    graphics/02fig59.gif

    Figure 2.60. You can change the default namespace through the project properties dialog. However, this doesn't change the namespace names in files that have already been added to the project, only in new files. You can also set this property to blank.

    graphics/02fig60.gif

  • Namespaces can be nested. That means you can declare a namespace inside another namespace declaration ( Figure 2.61 ).

    Figure 2.61 Too much nesting results in carpal tunnel syndrome. Some classes in .NET are nested five or six namespaces deep. Make sure you have a comfortable wrist pad.
     namespace  Nesting  {    namespace  CarpalTunnel  {      namespace  Syndrome  {           class Wrist           {           }       }    } } //class name is now //  Nesting.CarpalTunnel.Syndrome.Wrist  
  • Creating namespaces is optional, but it makes good programming sense to group related classes. The namespace serves as a prefix to every class name, so that if your namespace names are WidgetsUSA and Banking and your classes are Checking and Savings then your class names will end up being WidgetsUSA.Banking.Checking and WidgetsUSA.Banking.Savings ( Figure 2.62 ).

    Figure 2.62 The namespace names become part of the class names.
      namespace WidgetsUSA   {   namespace Banking   {  public class Checking      {        //code omitted for simplicity      }      public class Savings      {        //code omitted for simplicity      }    } } class App {    void Program()    {  WidgetsUSA.Banking.Checking  acct1 =  new WidgetsUSA.Banking.Checking  ();  WidgetsUSA.Banking.Savings acct2  =       new  WidgetsUSA.Banking.Savings  ();    } } 
  • The using keyword in C# lets you omit the namespace name of the class when referring to the class in code ( Figure 2.63 ). When you use using you can refer to the classes by their class names. All you're doing is saving keystrokes later on. You can always skip using and just use the full name of the classes when you write code.

    Figure 2.63 When you use the using command, you can omit the namespace name and just type the class name.
      using WidgetsUSA.Banking;  namespace WidgetsUSA {    namespace Banking    {      public class Checking {}      public class Savings {}    } } class App {    void Program()    {  Checking  acct1 = new  Checking  ();  Savings  acct2 = new  Savings  ();    } } 
  • When you use the keyword using you can assign an alias to the namespace name ( Figure 2.64 ). This can help in two ways. First, it helps the compiler in cases where two classes with the same name may be in two separate namespaces. Imagine a class named MyArray. It is possible for the class to be defined in two namespaces: MyMath and MyCollections. If the developer writes :

     using MyMath; using MyCollections; 

    and then uses the class name MyArray directly, the compiler can't tell which class the developer is referring to. Second, it helps with intellisense. When you type the prefix, the Visual Studio .NET editor displays the list of classes in the namespace ( Figure 2.65 ).

    Figure 2.64 An alias can be assigned to a namespace name in order to resolve ambiguity in cases where the same class name may appear in two different namespaces.
      using ma=MyMath;   using ca=MyCollections;  namespace MyMath {    public class MyArray {} } namespace MyCollections {    public class MyArray {} } public class MyApp {    public void DoWork()    {  ma.  MyArray arr1 = new  ma.  MyArray();    } } 
    Figure 2.65. Aliases in namespaces also help you in entering code with the help of intellisense, which displays choices for class names as you type. With an alias, intellisense will display the names of classes in the namespace.

    graphics/02fig65.gif




C#
C# & VB.NET Conversion Pocket Reference
ISBN: 0596003196
EAN: 2147483647
Year: 2003
Pages: 198
Authors: Jose Mojica

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net