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?
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.
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:
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:
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)
The whole point of namespaces is that they are used to organise your code:
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.
I've never seen your number 2 used. It reminds me of VB6 Modules.
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.