How to organize class files in C #

I am working on a simple project and I have created several classes, interfaces, one static class, etc. I ask how to organize these files in namespaces. Is there any good practice for this, or should I just follow the logic of my program. Currently, I think I should move the interfaces to one namespace and all classes to others. So you can advise me. I am really interested to know how to distinguish my files.

A good day:)

+4
source share
5 answers

You must group your code in the namespace with other types that have the highest cohesion . That is, the types of groups together when they perform common functionality. The type of cohesion you propose is logical coherence and is actually a rather weak form of cohesion.

+3
source

Namespaces are mainly intended for large projects. Since you are working on a β€œsimple project,” I suggest you use a single namespace for the entire application. Since everything in C # must be a type or a type member (i.e. there are no global variables or methods), the types you create (objects, classes, interfaces, enumerations, etc.) are usually good enough organization for a small project.

For a few larger projects, I suggest putting each level in its own namespace.

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

+2
source

In a specific namespace, you should put everything related to a certain issue. For example, you should put all materials related to string manipulations in a separate namespace, for example. com.server.string.

This is very important, especially if you have a class with names that exist in other namespaces.

+1
source

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

As a rule, I try to create folders for listings, structures, 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 a file; at another time, you let your nesting take care of the naming convention.

A good dumb rule is that you should be able to find what you are looking for quickly, and more importantly, someone who has not seen the project should quickly find their way.

Keep in mind that you never add more than one item to a single file. Never put two classes in the same file, never add enumerations at the end of a class file, etc.

+1
source

You are confusing files with classes. You can create folders in Visual Studio to organize your files. This way you can group interfaces and classes (which I usually do). VS automatically places new classes for which the file is located in these folders in the namespace with the same name. This is usually not what you want (I don’t know how to disable it, so I can’t do it).

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

+1
source

Source: https://habr.com/ru/post/1347155/


All Articles