Class.Class vs Namespace.Class for top level gener

2020-02-06 08:33发布

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?

7条回答
疯言疯语
2楼-- · 2020-02-06 09:26

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

查看更多
登录 后发表回答