Stoimen
Stoimen

Reputation: 669

How to organise class files in C#

I am working on a simple project and I have created several classes, interfaces, one static class and so on. What I am asking is, how to organise this files into namespaces. Is there any good practice for this or I should just follow the logic of my program. I am currently thinking that I should move the interfaces into one namespace and all the classes into another. So what can you advise me. I am really curious to find out the best way to separate my files.

Have a nice day :)

Upvotes: 4

Views: 639

Answers (5)

Jeffrey L Whitledge
Jeffrey L Whitledge

Reputation: 59443

Namespaces are mainly for the benifit of large projects. Since you are working on a "simple project", I suggest that you use a single namespace for the entire application. Since everything in C# must be a type or a member of a type (i.e., there are no global variables or methods), the types that you create (objects, classes, interfaces, enums, etc.) are usually a good-enough organizing feature for a small project.

For slightly larger projects, I suggest putting each tier into its own namespace.

For even larger projects, namespaces should be a logical grouping of related types or subsystems, according to preference.

Upvotes: 2

Jouke van der Maas
Jouke van der Maas

Reputation: 4308

You are confusing files with classes. You can create folders in Visual Studio to organize your files. That way you can group interfaces and classes (which is what I usually do). VS will automatically put new classes for which the file is in those folders in the namespace of the same name. This is usually not what you want (I don't know how to turn it off, so I can't help you with that).

I agree with the other answers here that you should group types based on what they do, not on what kind of language construct they are.

Upvotes: 1

NKCSS
NKCSS

Reputation: 2746

The only reason to split your code in files is to make your code maintainable.

As a general rule of thumb, I tend to create folders for enum's, struct's, models, controllers, etc. Depending on the size of the solution, you keep nesting in groups after that.

Sometimes it makes sense to just put the entire namespace in the file, other times, you let your nesting take care of the naming.

A good rule of tumb is that you should be able to find what you are looking for quicky, and, more importantly, someone who hasn't seen the project, should find his way around quickly.

One thing to keep in mind is that you never put more then one thing in one file. Never put two classes in the same file, never append enums at the end of a class file, etc.

Upvotes: 1

Mark H
Mark H

Reputation: 13897

You should group your code in namespace with other types which have the highest cohesion. That is, group types together when they perform common functionality. The type of cohesion you're suggesting is logical cohesion, and is really a rather weak form of cohesion.

Upvotes: 3

TOUDIdel
TOUDIdel

Reputation: 1304

Into specific namespace you should put everything which concerns some matter. For example all the stuff concerning string manipulations you should put into separate namespace, e.g. com.server.string.

It's very important especially in case you have class with names existing in other namespaces.

Upvotes: 1

Related Questions