Trying to Understanding Namespaces

  • Thread starter Thread starter Rob G
  • Start date Start date
R

Rob G

Hi,

I am having a problem finding a good explanation of exactly how Namespace
works. I am not clear on how it uses directories (if it does at all),
contains classes, after I complile a cs file how does a Namespace come into
play, or do I even need to have one?

Any info would be greatly appreciated.

Thanks so much.

-Rob
 
Hi Rob,
The work pretty simple. They make the name of the classes longer, minimizing
the chanches (but not avoiding) of type name clashes. There is not
directories and whatsoever involved.
 
Rob,

A namespace does not use directories as such. But you can think of them
as directories to hold and organize classes, structs, enums and in fact
all types.

It works in the same way as a file system. Imagine if you had all your
files in one folder. No file could have the same name, there would be
100,000's of files and it would be impossible to find anything. A
namespace is the same thing. If you put a class in the namespace
mycompany and your class was called myclass. Then if you were to use
that class in another project then you would call that class as
'mycompany.myclass'

Yes you do have to have your objects within a namespace, but its totally
up to you how you arrange it. Microsoft has set some recommend i
believe (if they have i am sure you can find them on MSDN).

Try to have your namespaces logical. for example System.Text contains
classes which can be used for text manipulation. System.IO namespace
contains Input/Output objects.
One thing to note- don't use the system namespace.

Hope this helps a little bit.

-Arran
 
Rob G said:
Hi,

I am having a problem finding a good explanation of exactly how Namespace
works. I am not clear on how it uses directories (if it does at all),
contains classes, after I complile a cs file how does a Namespace come into
play, or do I even need to have one?

Any info would be greatly appreciated.

Thanks so much.

-Rob

Directories and namespaces are loosely coupled:
When you add an item to a subdirectory of a project, the namespace clause
that is generated is: project namespace + directory
(project has namespace A.B, subdir = C: new item gets A.B.C)
But the compiler doesn't complain when you change that namespace.

Namespaces are a way to organise your classes. Without them it would be difficult
to assign a meaningful name to a new item or to find a defined item. (did I need
"myclass12345" or "myclass12346" ?)

Example: there is an Image class in namespace System.Drawing, but also
in System.Web.UI.WebControls and System.Web.UI.MobileControls.
All three are different classes.


Hans Kesting
 
Just to add to the excellent descriptions from other responders:

For your first namespace, use something like "MyCompany.MyApplication". Use
the same namespace for all your classes (including the forms).
Then, you are unlikely to need to care again.

If you create a DLL, and your app calls that DLL, you will want to use a
namespace naming convention that you will find useful.

Like "MyCompany.Library.SubjectArea" (e.g. if your company is called
CoolCoders and you are writing a DLL dealing with Graphics manipulation,
then something like: CoolCoders.Library.Graphics )

Then, to use it in your application, set a reference to the DLL that
contains the classes you want and put a "using" statement (if you use C#) or
an imports statement (if you use VB) in each file where you want to
reference the classes.

Also note: you can have as many namespaces declared in a single DLL as you
want. In fact, you can have more than one namespace declared in a single
file. It's up to you.

Good Luck,
--- Nick
 
And just a little reminder. Don't overdo with the namespaces. If you write a
library that is going to be used by other programmers and you ask them 'how
to organize the namespaces' they mostlikely will say 'Put all the classes in
one namespace...' :) at least I will answer that. It is really anoying if I
have to go back and forth to the documentation to see in which namespace the
class is defined. Oranize them logically, but keep granularity minimum.
 
All,

Thanks for the great responses. Everyone consolidated the information I need
in a way that I finally understood.

If I may ask another question...

The "Using" keyword is the way to use the classes in a namespace. Is that
correct?

Could anyone give me a quick example of using a Namespace?

Thanks again.

-Rob
 
Having the Using is optional. Say you want to run a process using the
Process Class which is stored in the System.Diagnostics Namespace. You
could create the class like this:

System.Diagnostics.Process proc = new System.Diagnostics.Process();

However if you put "Using System.Diagnostics" then you can create the
Process class like this:

Process proc = new Process();

Having the 'Using' is like shorthand.
 
Back
Top