When dealing with style, it is always more useful to take either an official style if one exists, or to use the one commonly adopted by the community of developers writing in a specific language.
You're lucky, for C#, there is an official style created by Microsoft. Another good thing is that Microsoft also developed a tool called StyleCop which checks if your code is valid.
Two of the rules are relevant to your question:
- SA1210: Using directives must be ordered alphabetically by namespace.
- SA1208: System using directives must be placed before other using directives.
In other words, namespaces starting by
System. always come first, ordered alphabetically, and all other namespaces, also ordered alphabetically, come second. There is no empty line between the two. The code you quoted in your question is a perfect illustration of this style.
The one I suggest has a few benefits:
It can be applied automatically instead of a manual choice of which of the usings is more important. Actually, this is the one which is enforced automatically in Visual Studio when you ask to reorder the usings. Having one thing less to think about during development allows you to focus on things that really matter, such as the quality of your code.
It can be checked (for instance during a commit).
There would be no pointless discussions among your colleagues about the importance of the respective namespaces.
There is, however, a major drawback:
- If you rename a namespace, it would often happen that it would create havoc in your well-ordered lists of usings in other files. While Visual Studio makes it very easy to reorder namespaces in a single file, it doesn't help at all when it comes to applying the order in all the files of a project (and if I'm not mistaken, Visual Studio won't bother fixing the order of the namespaces when renaming one).
The one I suggest has the following benefits:
Here again, Visual Studio support comes up handy. It can be difficult if I have to waste my time reordering the namespaces by hand (or slow my IDE by installing CodeRush or Resharper).
A newcomer won't have to learn a different style from the one commonly used by the industry. This, alone, is enough to avoid this style.
StyleCop checks this style automatically. If Olivier's style has to be used, one should develop a custom rule for StyleCop. Nothing impossible, but still additional work to do.
The drawback is:
- The relative mix between in-house namespaces and third-party namespaces. This could be an issue if in-house namespaces are named inconsistently; if they are all prefixed by the name of your company, they are necessarily together when ordered alphabetically anyway.