Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET namespaces

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.

like image 832
Jacob Schoen Avatar asked Sep 11 '08 02:09

Jacob Schoen


People also ask

What is namespace in .NET with example?

The namespace keyword is used to declare a scope that contains a set of related objects. You can use a namespace to organize code elements and to create globally unique types. namespace SampleNamespace; class AnotherSampleClass { public void AnotherSampleMethod() { System. Console.

What are namespaces in C#?

Namespaces are used in C# to organize and provide a level of separation of codes. They can be considered as a container which consists of other namespaces, classes, etc. A namespace can have following types as its members: Namespaces (Nested Namespace) Classes.

What is a namespace example?

In an operating system, an example of namespace is a directory. Each name in a directory uniquely identifies one file or subdirectory. As a rule, names in a namespace cannot have more than one meaning; that is, different meanings cannot share the same name in the same namespace.

Can you have 2 namespaces in C#?

In c#, we can define and access multiple namespaces in our application with using keyword. To access the custom namespace classes, we need to import the custom namespace with using keyword and need to create an instance for that classes in our application.


1 Answers

I can't claim that it's a best practice, but I often see files organized in a directory hierarchy that mirrors the namespace. If it fits your mental model of the code better, then do so - I can't think of any harm. Just because the .NET model doesn't enforce relationships between namespaces, projects, and directory structure doesn't mean you can't have such relationships if you want to.

I'd be a little leery of breaking up the code into more projects than you need, as this can slow compilation and add a little bit of overhead when you have to manage multiple assemblies.

EDIT: Note that this question is nearly a duplicate of should the folders in a solution match the namespace?

like image 154
Blair Conrad Avatar answered Sep 27 '22 20:09

Blair Conrad