Share via


Sort Usings

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

    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 Advanced, C#, Text Editor, Options Dialog Box

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;

Remarks

Preprocessor Directives

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

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 statement below because there is no carriage return between the statement and the comment. To prevent this behavior, add an extra carriage return after the comment.

See Also

Concepts

Organizing Using Statements

Removing Unused Usings

Reference

Advanced, C#, Text Editor, Options Dialog Box

using Directive (C# Reference)

extern alias (C# Reference)