.Class vs Namespace.Class class for top-level shared libraries?

Which one is more acceptable (best) ?:

namespace NP public static class IO public static class Xml ... // extension methods using NP; IO.GetAvailableResources (); 

against

 public static class NP public static class IO public static class Xml ... // extension methods NP.IO.GetAvailableResources (); 

Also for #2 , the code size is controlled using partial classes, so each nested class can be in a separate file, the same for extension methods (except that there is no nested class for them)

I prefer #2 for two reasons, such as the ability to use type names that are already commonly used, such as IO , which I don't want to replace or collide with.

Which one do you prefer? Any pros and cons for everyone? What is the best practice for this case?

EDIT: Will there also be a performance difference between the two?

+4
source share
7 answers

I would say # 1. Because when you combine many classes into one static class, you do the same thing as the namespace. That's why I would say that it is best to let namespaces do this for you.

In this case, you can also get rid of the need to write "NP" before everything, adding usage if you want. I think you should either nest your namespaces so that they do not interfere with our use, but rather describe namespace names than IO.

Most often, the best practice is what Microsoft does, and I have never seen them do # 2

+3
source

I prefer # 1 because it does not require me to call through 1 class to get to another. I think this is a little confusing, because in shared objects we mean member classes, methods, etc., which are directly related to the objects created when the class is created. This means that you are not really following the principles of OOP. Microsoft also says # 1 is best practice.

+2
source

I have never seen your number 2. It reminds me of VB6 modules.

+1
source

the whole point of the namespace is that they are used to organize your code :

Namespaces are heavily used in C # programs in two ways. First, .NET Framework classes use namespaces to organize their many classes. Secondly, declaring your own namespaces can help limit the amount of class and method names in larger programming projects.

When you look at the .NET platform, you can see that almost everything is structured, like your first example (namespace), and almost nothing is structured like your second example (nested types). Rare cases in which nested types are used are when they are so closely related to the external type that they are essentially part of this, but should be a separate class for reasons such as code reuse.

So, if by "best practice" you mean "using things for the purpose for which they were developed", "most similar to the .NET platform" and "most closely related to the .NET development paradigms", then there is no competition; Use namespaces for the organization, not nested types.

As for your editing - no, the difference in performance will not matter in the real world.

+1
source

I prefer # 1 and let extraspace do it. Like so many others, it is very difficult to introduce classes into other classes.

Also, when you do more complex things, such as mirroring or using something like a vendor model, having nested classes in your actual vendor section or the goal you are trying to achieve becomes hairy.

Finally, testing nested classes and interfaces makes testing difficult.

+1
source

I think you should avoid open (open) nested classes and interfaces, or at least that's what Microsoft FxCop would say. So the first is better.

Edit: (yes, changed to first, I should not respond to SO when I am tired of fatigue)

0
source

I actually often use # 2. I will go why after the next line. But in your case with empty static classes, namespaces are more ideal, because that's what they are for.

However, if you have a use case when you write out new classes, sometimes it might make sense to nest these derived classes.

For example, you could:

 public abstract class Vehicle { int NumberOfWheels; } public class SportsCar : Vehicle { } 

Now you can put both of them in the vehicle namespace so that the parent is not cluttered with all the different derived classes. However, this is a bad idea.

Instead, embedding them gives you all the benefits:

 public abstract class Vehicle { int NumberOfWheels; public class SportsCar : Vehicle { } } 
0
source

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


All Articles