473,395 Members | 1,677 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

namespaces - what's the point?

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?
Nov 21 '05 #1
9 1220
Patty O'Dors <Pa********@discussions.microsoft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 21 '05 #2
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.

"Patty O'Dors" <Pa********@discussions.microsoft.com> wrote in message
news:8D**********************************@microsof t.com...
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?

Nov 21 '05 #3
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

"Patty O'Dors" <Pa********@discussions.microsoft.com> wrote in message
news:8D**********************************@microsof t.com... 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?

Nov 21 '05 #4
Patty O'Dors <Pa********@discussions.microsoft.com> wrote:
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.
No, but I only have an average of 15 files in each project. (That's
increasing with the complexity of the projects I do, though...)
I did try organising them like directories, but to be quite frank I found
the total time I spent each day just typing the word 'using' was becoming a
bit laborious, so I quit..... I might bite the bullet again.


I suspect you will have to, yes. Any project of significant complexity
is going to become a pain if you don't use namespaces.
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.


A project containing ALL my generic code wouldn't be workable, as some of
the code's relevant to different type of projects, for example console
application, web application, windows forms.
Or is it that you're saying I should make DLLs out of it, and not bother
compiling it in?


That's exactly what I'm suggesting, yes.
I suppose I have my VB6 background to blame for that, as
it's handling of DLL references is far worse than .NET, to such an extent
that VB6 DLLs are unworkable at our office, but .NET DLLs are seamless. I
think that could be the way, really: can you just confirm to me one thing
though: when I reference a DLL, should I get it out of the bin\release folder
(rather than obj\release)? And it then copies it to the client project's bin
directory automatically?
And when i'm building a DLL with a test project, I should reference it by
using the 'projects' tab (when the references gives me the choice of '.NET',
'COM' or 'project')?


Definitely use "Project" - that way your previous question doesn't
matter (it gets it out of the relevant directory itself).

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 21 '05 #5
Your project files are not analogous to namespaces, classes are. You can
have 1000 files and 10,000,000 classes and decide to put all those classes
into any number of namespaces.

Think of it this way, there is a Textbox class for use in ASP.NET projects
and there is a Textbox class for use in Windows Forms projects. It doesn't
make sense to have a textbox be called anything other than a Textbox, so you
have to have 2 classes with the same name. But if you have 2 classes with
the same name, how do you know which is which? Enter namespaces. They
qualify a class and tell us where the actual class definition is for a name.
"Patty O'Dors" <Pa********@discussions.microsoft.com> wrote in message
news:0A**********************************@microsof t.com...
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.


No, but I only have an average of 15 files in each project. (That's
increasing with the complexity of the projects I do, though...)
I did try organising them like directories, but to be quite frank I found
the total time I spent each day just typing the word 'using' was becoming
a
bit laborious, so I quit..... I might bite the bullet again.
> 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.


A project containing ALL my generic code wouldn't be workable, as some of
the code's relevant to different type of projects, for example console
application, web application, windows forms.
Or is it that you're saying I should make DLLs out of it, and not bother
compiling it in? I suppose I have my VB6 background to blame for that, as
it's handling of DLL references is far worse than .NET, to such an extent
that VB6 DLLs are unworkable at our office, but .NET DLLs are seamless. I
think that could be the way, really: can you just confirm to me one thing
though: when I reference a DLL, should I get it out of the bin\release
folder
(rather than obj\release)? And it then copies it to the client project's
bin
directory automatically?
And when i'm building a DLL with a test project, I should reference it by
using the 'projects' tab (when the references gives me the choice of
'.NET',
'COM' or 'project')?
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


Mmmm... no, I don't think I'll do that.


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.

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

Nov 21 '05 #6
Just one more note:
"Patty O'Dors" <Pa********@discussions.microsoft.com> wrote in message
news:0A**********************************@microsof t.com...
I think that could be the way, really: can you just confirm to me one thing though: when I reference a DLL, should I get it out of the bin\release folder (rather than obj\release)? And it then copies it to the client project's bin directory automatically?

If you are referencing an assembly within the same solution, you can use
"project" references. However, if you do create assemblies with generic
reusable code, sooner or later you will need to reference the compiled DLL.
In that case, ALWAYS reference out of the 'obj' directory. Use the 'bin'
directory to run your debugger or copy files for "xcopy deployment". Never
use this directory for making a reference to a compiled assembly.

--- Nick Malik

Nov 21 '05 #7
Definitely use "Project" - that way your previous question doesn't
matter (it gets it out of the relevant directory itself).
Interesting... I do like the way the 'project' setting abstracts the need to
choose between obj and bin, but are you saying to use that all the time, even
when I'm not necessarily working on a test project, but actually using the
DLL, knowing that it works correctly? That sounds fine, the only issue being
that if I want to give a co-worker one of my DLLs or vice versa, they need
all the project files, not just the DLL file itself?

Can source control get round this?

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

Nov 21 '05 #8
Patty O'Dors <Pa********@discussions.microsoft.com> wrote:
Definitely use "Project" - that way your previous question doesn't
matter (it gets it out of the relevant directory itself).


Interesting... I do like the way the 'project' setting abstracts the need to
choose between obj and bin, but are you saying to use that all the time, even
when I'm not necessarily working on a test project, but actually using the
DLL, knowing that it works correctly? That sounds fine, the only issue being
that if I want to give a co-worker one of my DLLs or vice versa, they need
all the project files, not just the DLL file itself?

Can source control get round this?


If you've just got the DLL, add a reference to that. If you've got the
project in the same solution, add a reference to the project instead.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 21 '05 #9
no. Source control doesn't get around this.

If you are sharing the DLL with another project, on a machine that has the
source, refer to the OBJ directory.
If you are sharing the DLL with another coworker, and they don't have the
source, just have them refer to the DLL.

--- Nick

"Patty O'Dors" <Pa********@discussions.microsoft.com> wrote in message
news:97**********************************@microsof t.com...
Definitely use "Project" - that way your previous question doesn't
matter (it gets it out of the relevant directory itself).
Interesting... I do like the way the 'project' setting abstracts the need

to choose between obj and bin, but are you saying to use that all the time, even when I'm not necessarily working on a test project, but actually using the
DLL, knowing that it works correctly? That sounds fine, the only issue being that if I want to give a co-worker one of my DLLs or vice versa, they need
all the project files, not just the DLL file itself?

Can source control get round this?

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

Nov 21 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: Steven Bethard | last post by:
In the "empty classes as c structs?" thread, we've been talking in some detail about my proposed "generic objects" PEP. Based on a number of suggestions, I'm thinking more and more that instead of...
2
by: Raymond Lewallen | last post by:
It could help if you understood the project I work on a bit, but I can't tell you anything about it, so do the best you can under that pretense. Lets say you have a global assembly supporting...
2
by: Mike Morse | last post by:
What see sample that show xs:element where the xs namespace = http://www.w3.org/2001/XMLSchema However, I see another example with xsi: where xsi = http://www.w3.org/2001/XMLSchema-instance ...
2
by: Beeeeeeeeeeeeves | last post by:
Does C# really HAVE to have namespaces? Isn't there any way I can turn them off? I did a bit of dabbling in VB.NET but have decided not to switch to it permanantly as I've got too much pre-written...
9
by: Patty O'Dors | last post by:
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...
17
by: clintonG | last post by:
Using 2.0 with Master Pages and a GlobalBaseClass for the content pages. I understand the easy part -- the hierarchical structure of a namespace naming convention -- but the 2.0 IDE does not...
11
by: Random | last post by:
I'm confused about the proper use and usefulness of namespaces. I beleive I understand the purpose is so the developer can put classes within namespaces to essentially organize your code. And I...
36
by: Wilfredo Sánchez Vega | last post by:
I'm having some issues around namespace handling with XML: >>> document = xml.dom.minidom.Document() >>> element = document.createElementNS("DAV:", "href") >>> document.appendChild(element)...
6
by: douglass_davis | last post by:
Is the whole System namespace automatically imported?
4
by: =?Utf-8?B?Q2Fpcm4=?= | last post by:
I am new to Xpath but wish to parse an XML document for nodes which contain a child node with a particuale value. Please does anybody have any clues as to why I never get any returns in the root...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.