Skip to content
Ian Griffiths edited this page Mar 6, 2014 · 6 revisions

Order Usings enables fine-grained control over ordering rules for C# using directives. It is currently available only as a plug-in for ReSharper 8.1.

(You can install it from ReSharper's Extension Manager dialog. Just type "Order Usings" into the search box and it should come up under "Ordering and Spacing for using directives".)

Tools such as ReSharper and StyleCop provide tools to help enforce consistency in the way C# using directives are arranged. However, they can be frustratingly narrow - if you happen to want all the namespaces starting with System at the top, followed by everything else, with both sets sorted into alphabetical order, then they are fine, but if you want anything else, these tools don't help.

For example, if you want all of the using directives for namespaces specific to your project to appear after everything else, the only way to ensure that with these other tools is to pick a project name that starts with a letter near the end of the alphabet...

With Order Usings, you can have as many separate groups as you like, and you can choose whether or not to put spaces between them. For example, suppose you want something like this:

using System;
using System.Collections.Generic;
using Microsoft.WindowsAzure.Storage.Table;

using Aspose.Slides;
using Aspose.Slides.Pptx;
using Newtonsoft.Json;

using MyApp.Messages;
using MyApp.Transformations;

The overall pattern here is that I want three groups:

  1. Microsoft-supplied libraries
  2. Other libraries
  3. My own application's namespaces

but this is further complicated by the fact that the first group consists of a mixture of namespaces starting with System and Microsoft. I want all the System ones first because that's how things usually look.

To do this, I can define some groups, along with a couple of spacing elements:

<Groups xmlns="http://schemas.interact-sw.co.uk/OrderUsings/2014">
  <Group Priority="1" NamespacePattern="System*" />
  <Group Priority="1" NamespacePattern="Microsoft*" />
  <Space />
  <Group Priority="9999" NamespacePattern="*" />
  <Space />
  <Group Priority="1" NamespacePattern="MyApp*" />
  <Space />
  <Group Priority="9999" NamespacePattern="*" AliasPattern="*" Type="Alias" />
</Groups>

You enter this configuration with ReSharper's Options dialog. The plug-in adds an "Order Usings" item under Tools. (It uses ReSharper's built-in settings handling to store this, so it'll end up in the same place as any other ReSharper settings. So as usual, you can choose which layer to store it in, e.g. shared team settings if you want everyone on the project to use the same settings, or your machine settings if you prefer to do things that way.)

Clone this wiki locally