Alex Rivera | Logout

.NET namespaces

Asked 2008-09-11T02:26:45.327
9

My background is primarily as a Java Developer, but lately I have been doing some work in .NET. So I have been trying to do some simple projects at home to get better at working with .NET. I have been able to transfer much of my Java experience into working with .NET (specifically C#), but the only thing that has really perplexed me is namespaces.

I know namespaces are similar to Java packages, but as from what I can tell the main difference is that with Java packages they use actual file folders to show the seperation, while in .NET it does not and all the files reside in a single folder and the namespace is simply declared in each class.

I find this odd, because I always saw packages as a way to organize and group related code, making it easier to navigate and comprehend. Since in .NET it does not work this work this way, overtime, the project appears more overcrowded and not as easy to navigate.

Am I missing something here? I have to be. Should I be breaking things into separate projects within the solution? Or is there a better way to keep the classes and files organized within a project?

Edit: As Blair pointed out this is pretty much the same question asked here.

Edit
Report

2 Answers

4

Namespaces are a logical grouping, while projects are a physical grouping.

Why is this important? Think about .NET 2.0, 3.0, and 3.5. .NET 3.0 is basically .NET 2.0 with some extra assemblies, and 3.5 adds a few more assemblies. So for instance, .NET 3.5 adds the DataPager control, which is a web control and should be grouped in System.Web.UI.WebControls. If namespaces and physical locations were identical, it couldn't be because it's in a different assembly.

So having namespaces as independent logical entities means you can have members of several different assemblies which are all logically grouped together because they're meant to be used in conjunction with each other.

(Also, there's nothing wrong with having your physical and logical layouts pretty similar.)

answered 2008-09-12T03:30:14.043
3

the difference is that .net namespaces have nothing much to do with java packages.

.net namespaces are purely for managing declarative scope, and have nothing to do with files, projects or their locations.

it's very simple everything declared in a particular namespace is accessible when you include a 'using' to that namespace.

very easy.

the choice of name and whether or not/how many '.' seperators you use is entirely up to you.

VS defaults to adding .foldernames to your namespaces just to try and be helpful.

this article explains namespaces quite well: http://www.blackwasp.co.uk/Namespaces.aspx

It also has an example naming convention toward the end, although your name convention is your call! ;)

that said, most places i've worked at and people i've worked with start with company name which is sensible, as it makes typenames for that company distinct, (separate from other, libraries, vendors, opensource projcts etc.)

answered 2009-12-04T14:24:01.627

Your Answer