Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / August 2004

Tip: Looking for answers? Try searching our database.

namespaces - what's the point?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Patty O'Dors - 13 Aug 2004 13:15 GMT
Hi
Can somebody please tell me what namespaces are actually for?

I notice that when I start a new project in C#, it puts everything in a
namespace of the same name as the project.
I found them a bit annoying as when I save a class's .cs file to my 'generic
code' folder, and then put it in another project, I have to manually go and
change the namespace name. This I find a bit of a PITA.
I used VB.NET for a bit and found it didn't have namespaces by default, but
that it can have them if it wants. I also found that although C# has them by
default, I don't actually have to have them in order to compile.
So - they're not necessary, and seeing as I don't like them, why can't I
turn them off?

And what's the logic behind them being optional in both languages, but on by
default in C# yet off by default in VB.NET?
Jon Skeet [C# MVP] - 13 Aug 2004 13:36 GMT
> Can somebody please tell me what namespaces are actually for?

It means that:
o If you absolutely have to, you can have two types with the same names
 in different namespaces. It's worth trying to avoid this wherever
 possible though.
o You can easily see what namespaces a class uses, assuming it doesn't
 explicitly use full type names, through the using directives at the
 top of the class.
o Intellisense will only display the relevant type names, rather than
 showing *every* type.
o When creating your own types, it gives the reader a clear idea of the
 kind of class it is - for instance, if a type is in a namespace with
 a name like "BusinessLogic" then it's going to be related to business
 logic.

Put it this way: do you keep absolutely everything in one directory on
your computer? Use namespaces in a similar way to how you'd use
directories.

> I notice that when I start a new project in C#, it puts everything in a
> namespace of the same name as the project.

You can change the root namespace of the project - I usually change it
to an empty string so that my namespace structure then exactly matches
the directory structure.

> I found them a bit annoying as when I save a class's .cs file to my 'generic
> code' folder, and then put it in another project, I have to manually go and
> change the namespace name. This I find a bit of a PITA.

If you're reusing code, you should have a project containing all that
code, rather than duplicating it in each project.

> I used VB.NET for a bit and found it didn't have namespaces by default, but
> that it can have them if it wants. I also found that although C# has them by
> default, I don't actually have to have them in order to compile.
> So - they're not necessary, and seeing as I don't like them, why can't I
> turn them off?

Well, you can edit the template that C# uses to create a new class if
you want - look at

C:\Program Files\Microsoft Visual Studio .NET 2003\VC#\VC#Wizards
\CSharpAddClassWiz\Templates\1033\NewCSharpFile.cs

You can edit that and similar templates to get rid of the namespace
declaration if you really want.

> And what's the logic behind them being optional in both languages, but on by
> default in C# yet off by default in VB.NET?

Well, you will always be using namespaces to some extent in C#. You
just don't have to put types in a namespace other than the "empty
name" one.

Personally I'd recommend always using a namespace though.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Marina - 13 Aug 2004 14:40 GMT
Would you put every single file on your system into one folder? No, you
origanize them in meaningful ways, with folders that have subfolders etc.

If you didn't, not only would you have thousands of files all in one place
(making it virtually impossible to find anything), you would only be able to
use a filename once, since they are all in the same folder.

Same reasoning goes for namespaces.

> Hi
> Can somebody please tell me what namespaces are actually for?
[quoted text clipped - 12 lines]
> And what's the logic behind them being optional in both languages, but on by
> default in C# yet off by default in VB.NET?
Jay B. Harlow [MVP - Outlook] - 13 Aug 2004 15:03 GMT
Patty,
In addition to the other comments.
> I used VB.NET for a bit and found it didn't have namespaces by default, but

VB.NET has namespaces and the are ON by default!!! :-)

The difference between VB.NET & C# is that VB.NET puts the namespace as a
project level setting, while C# includes the namespace in the source file.
You can use Project - Properties to change the Root Namespace used in
VB.NET, which is normally the same as the project (just as C# does!)

If you use ILDASM or reference either assembly (VB.NET & C#) you will see
that all classes are in a namespace!

Hope this helps
Jay

> Hi
> Can somebody please tell me what namespaces are actually for?
[quoted text clipped - 12 lines]
> And what's the logic behind them being optional in both languages, but on by
> default in C# yet off by default in VB.NET?

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.