Organizing Using Directives

The Organize Usings options in the Visual Studio integrated development environment (IDE) provide an easy way to sort and remove using and extern declarations without changing the behavior of the source code. Over time, source files may become bloated and difficult to read because of unnecessary and unorganized using directives. The Organize Usings options compact source code by removing unused using directives and improves readability by sorting them.

To see the available options in the Visual Studio IDE, on the Edit menu, point to IntelliSense, and then point to Organize Usings. The IDE provides the following options to organize and remove usings directives:

The Sort Usings option in the Visual Studio user interface improves the readability of source code by alphabetizing and organizing using directives, using aliases, and extern aliases in the following order:

  1. extern aliases

  2. using directives

  3. using aliases

    Note Note

    By default, Visual Studio sorts using directives that begin with System before other using directives. You can modify Sort Usings to sort all using directives alphabetically. For more information, see Options, Text Editor, C#, Advanced

There are two ways to call the operation:

  • Main Menu - On the Edit menu, point to IntelliSense, point to Organize Usings, and then click Sort Usings.

  • Context Menu - Right-click anywhere inside the code editor, point to Organize Usings, and then click Sort Usings.

The following example shows the outcome of performing Sort Usings on source code.

Before

After

extern alias ApressLibrary2;

extern alias ApressLibrary1;

using aio = apressio;

using System.Collections;

using Microsoft.CSharp;

using System;

using apressio = Apress.IO;

extern alias ApressLibrary1;

extern alias ApressLibrary2;

using System;

using System.Collections;

using Microsoft.CSharp;

using aio = apressio;

using apressio = Apress.IO;

Sort Usings will not sort when preprocessor directives separate directives or aliases. For example, the following code will not be sorted.

// Not sorted because preprocessor directives separate the using directives.

using System.Linq;

#region MyRegion

using System.Collections.Generic;

using System;

#endregion

using System.Collections;

However, the following example will be sorted.

// Sorted because pre-processor directives do not separate using directives

#region MyRegion

using System.Collections;

using System;

using System.Collections.Generic;

#endregion

Comments that are directly above or inline with a directive are grouped with the directive during sorting. The following example illustrates this behavior.

Before

After

// © Contoso, Ltd

using apressdata = Apress.Data;

using aio = apressio;

using System.Collections;

using System; // using System;

using System.Collections.Generic;

// using System.Text

using System.Text;

using apressio = Apress.IO;

// The End

using System; // using System;

using System.Collections;

using System.Collections.Generic;

// using System.Text

using System.Text;

using aio = apressio;

// © Contoso, Ltd

using apressdata = Apress.Data;

using apressio = Apress.IO;

// The End

In the example above, the comment // © Contoso, Ltd is grouped and sorted with the using directive below because there is no carriage return between the directive and the comment. To prevent this behavior, add an extra carriage return after the comment.

The Remove Unused Usings option in the Visual Studio user interface removes using directives, using aliases, and extern aliases that are not used in the source code. There are two ways to call the operation:

  • Main Menu - On the Edit menu, point to IntelliSense, point to Organize Usings, and then click Remove Unused Usings.

  • Context Menu - Right-click anywhere inside the code editor, point to Organize Usings, and then click Remove Unused Usings.

    Note Note

    If you perform Remove Unused Usings on source code that does not build, some required using directives may be removed.

The following example shows the outcome of performing Remove Unused Usings on source code.

Before

After

using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("test");
        }
    }
}
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("test");
        }
    }
}

In the previous example, only System is used later in the source code. The other using directives, including the duplicate System using directive, are removed.

Remove Unused Usings only removes unused directives and aliases that are in the active block. The following example illustrates this behavior:

Before

After

#define DEBUG

#if DEBUG

using System;

using System.Collections.Generic;

using System.Linq;

#else

using System.Text;

#endif

namespace ConsoleApplication1

{

class Program

{

static void Main(string[] args)

{

List<int> myList = new List<int> { 1, 2, 3 };

Console.WriteLine(myList);

}

}

}

#define DEBUG

#if DEBUG

using System;

using System.Collections.Generic;

#else

using System.Text;

#endif

namespace ConsoleApplication1

{

class Program

{

static void Main(string[] args)

{

List<int> myList = new List<int> { 1, 2, 3 };

Console.WriteLine(myList);

}

}

}

In the previous example, both System.Text and System.Linq are not used. However, only System.Linq is removed because System.Text is not in the active block.

Remove Unused Usings removes a comment only if the comment is between the tokens of a directive or alias that will be removed. Comments that appear before or after are not affected. The following example illustrates this behavior:

Before

After

using System;

/* Comment before remains */

using /* Comment between removed */ System.Linq;

// Comment after remains

namespace ConsoleApplication1

{

class Program

{

static void Main(string[] args)

{

Console.WriteLine("My Example");

}

}

}

using System;

/* Comment before remains */

// Comment after remains

namespace ConsoleApplication1

{

class Program

{

static void Main(string[] args)

{

Console.WriteLine("My Example");

}

}

}

In the previous example, System.Linq is removed. Only the comments between the directive's tokens are removed.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2015 Microsoft