How to organize classes in a Sharp C application?

Let's say that I have a Visual Studio project that should use a binary search tree data structure. Since .NET does not have a built-in BST data structure, I will have to add classes myself (for example, in this article ). This will be associated with several classes: Node, NodeList, BinaryTree, etc., so my question is that it would be better to add all these classes to a single .cs file, i.e. Attach them, or should each class be a separate file? It seems a little cleaner and it might be easier to reuse classes if they are all together in a single file, no?

I'm just trying to understand best practices for organizing and developing projects. Any advice or guidance you could give me would be appreciated.

+4
source share
7 answers

I recommend using one file for each type. There is no savings in limiting the number of files and one file per type, it really helps to find the type in your solution.

Quite often, files are organized into folders to help with organization levels (folders are often named along the lines of the namespace because they provide natural groupings of your types), although this is not always done.

+2
source

The easiest way to use binary trees is to download and link to the C5 Collections Library . Why reinvent the wheel?

In this case, the general rule - 1 widget --class / enum / struct / whatever - for the source file. Good practice (YMMV) is [as a rule] that the file system hierarchy reflect the namespace hierarchy, and the assemblies should be named so that they reflect the namespace.

+3
source

I would recommend using some of the built-in structures. I doubt you really need a binary tree.

If you write your own classes, each class should usually be in its own file. Use inner classes when you don't need to use them externally. (Make them private.) Inner classes are usually defined in the same file as the outer one.

+1
source

Like Jeff Yates, I would use one file for each type. Java applies this, .NET does not - but it seems to be suitable for well-organized solutions.

In addition, I would like to place all the BST-related files in a separate project in the same solution. If I understand you correctly, your application does not concern BST, it just uses them, right? If so, then here you can separate a separate project.

And also create well-named and well-defined namespaces. For example, MyApplication.Bst for files associated with BST, and MyApplication.UI for the external interface, for example.

+1
source

I would create a folder under the BinarySearch project and put them there.

0
source

I think that including multiple classes in one file often makes a project cumbersome. There is no performance benefit. If there are a very large number of classes that can be grouped, do this with a directory structure.

0
source

This question is very similar to another onen already in stackoverflow:

The standard practice is to have one class for each file, one notable exception is enumeration, delegates, and others.

0
source

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


All Articles