p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   C# (http://p2p.wrox.com/forumdisplay.php?f=22)
-   -   namespace (http://p2p.wrox.com/showthread.php?t=7449)

Ibn_Aziz December 19th, 2003 05:39 AM

namespace
 
Hi...

I do not understand what the keyword 'namespace' is used for. For example...

// csharp program
namespace Timer
{
     class .....
     ....
}

I am infact used to packages in Java..... to be able to create a package..... it must be added to the first line of the code for example............

// Java program
package newPackage;

class ...
....

Is it a similar concept..... if not how do we declare namespaces (packages?) in C#...........

thanks,
Ibn_Aziz

Ibn_Aziz December 21st, 2003 11:18 AM

Again answering my own questions.....

The namespace keyword is in reality used like the package keyword in JAva... basically to organise into a hierarchical structure of code....

ie

namespace ExampleOfNamespaces
{
     namespace ClassA
     {
          public ClassA
          {
                 ...
           }
     }

     namespace ClassB
     {
           public ClassB
           {
                ....
           }
      }
}


Then to be able to use class A, one would have to specify...

using ExampleOfNamespaces.ClassA;

Likewise to ab able to use class B one would have to specify

using ExampleOfNamespaces.ClassB;

One thing to bear in mind is that namepsace elements cannot explicitly be decalared as other than 'public'.

so this would cause an error....

namespace Trial
{
    private class box //error because only public allowed
    {
        ...
    }
}

Ankur_Verma December 25th, 2003 05:41 AM

Namespaces have a few more advantages than you have mentioned here. Like a namspace definition can span more than a single file. using namespaces you can resolve issues like how to use my Class A and smith's Class A in the same program, use the namesapces they are defines in to differentiate between them. I've been going through your queries and I would say that to appease the inquisitive mind that you possess you must get hold of a good book on C#. Professional C# would prove to be a good book for you.
Write back if there is anything else you wanna ask.

Ankur Verma
.Net and C++ Specialist
Wiley Tech Support


All times are GMT -4. The time now is 02:32 AM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.