Class.Class vs Namespace.Class for top level gener

2020-02-06 09:07发布

问题:

Which one is more acceptable (best-practice)?:

namespace NP
   public static class IO
   public static class Xml
   ...
   // extension methods

using NP;

IO.GetAvailableResources ();

vs

public static class NP
   public static class IO
   public static class Xml
   ...
   // extension methods

NP.IO.GetAvailableResources ();

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

I prefer #2, for a couple of reasons like being able to use type names that are already commonly used, like IO, that I don't want to replace or collide.

Which one do you prefer? Any pros and cons for each? What's the best practice for this case?

EDIT: Also would there be a performance difference between the two?

回答1:

I would say #1. Because when you bundle up lots of classes into one static class you do the same thing as a namespace is meant for. Which is why I would say it is best to let namespaces do that for you.

In that case you can also get rid of having to write "NP" in front of everything by adding using in case you want. I think you should either nest your namespaces so that they wont collide or use more describing namespace names than IO.

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



回答2:

I prefer #1 because it does not require me to call through 1 class to get to another. I think it makes things a bit confusing because in general objects are meant to have member classes, methods, and such that deal directly with the objects made by instantiating the class. That means you are not really following the principles of OOP. Microsoft also says #1 is best practice.



回答3:

I've never seen your number 2 used. It reminds me of VB6 Modules.



回答4:

The whole point of namespaces is that they are used to organise your code:

Namespaces are heavily used within C# programs in two ways. Firstly, the .NET Framework classes use namespaces to organize its many classes. Secondly, declaring your own namespaces can help control the scope of class and method names in larger programming projects.

When you look at the .NET framework itself, you can see that virtually everything is structured like your first example (namespaces), and virtually nothing is structured like your second example (nested types). The rare occasions that nested types are used are when they are so closely tied to the outer type that they are essentially part of it, but need to be a separate class for reasons such as code re-use.

So if by "best practice" you mean "using things for the purpose they were designed", "most like the .NET framework" and "corresponding most closely to .NET design paradigms" then there is no contest; use namespaces for organisation, not nested types.

Regarding your edit - no, there is no performance difference that will matter in the real world.



回答5:

I prefer #1 and let namespaces do it. Like others have said, it's very confusing to call into classes to get to other classes.

In addition, when you're doing more complex things like reflection or using something like a provider model, having nested classes being your actual provider section or the target you're attempting to reach becomes hairy.

Lastly, testing nested classes and interfaces make testing more difficult.



回答6:

I think you should avoid exposed (public) nested classes and interfaces, or at least that is what Microsoft FxCop would say. Thus, the first one is better.

Edit: (yes, changed to the first one, i shouldn't reply in SO when i'm dead tired)



回答7:

I actually find myself using #2 sometimes. I'll get into why after the next line. But in your case with empty static classes, namespaces are more ideal because that is what they are for.

However, if you have an use case where you are deriving new classes, some times it may make sense to nest these derived classes.

For example, you could have:

public abstract class Vehicle
{
    int NumberOfWheels;
}

public class SportsCar : Vehicle
{
}

Now, you may want to put them both under Vehicle namespace so that the parent is not cluttered with all the different derived classes. However, that's a bad idea.

Instead, nesting them gives you all the benefits:

public abstract class Vehicle
{
    int NumberOfWheels;

    public class SportsCar : Vehicle
    {
    }
}