C# Organize Usings: Visual Studio

In your C# program, you have a list of using directives at the top that allow you to access types easily. These may be in any order, but to make reading them easier, alphabetizing them is important.

This Visual Studio article describes the Organize Usings feature.

Organize Usings

Steps

To use the Organize Usings feature, right-click anywhere on the using directives and select Organize Usings. In these examples, we see the initial program. Then, we see the program with the Remove Unused Usings option invoked. Finally, we see the program with the Remove and Sort option used.

Initial program [C#]

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

class Program
{
    static void Main()
    {
	StringBuilder builder = new StringBuilder("Welcome");
	Int32 length = builder.Length;
    }
}

Selected "Remove Unused Usings" [C#]

using System.Text;
using System;

class Program
{
    static void Main()
    {
	StringBuilder builder = new StringBuilder("Welcome");
	Int32 length = builder.Length;
    }
}

Selected "Remove and Sort" [C#]

using System;
using System.Text;

class Program
{
    static void Main()
    {
	StringBuilder builder = new StringBuilder("Welcome");
	Int32 length = builder.Length;
    }
}
Programming tip

Recommendation. My suggestion is that you always just use "Remove and Sort" on a file that you have finished writing. If you are starting a new source code file, you should just leave the usings in any order because you probably don't want to remove the unused ones yet.

Summary

Visual Studio provides the Organize Usings feature, and this gives you the ability to clean up your source code so that it is easier to understand. On Dot Net Perls, I always use "Remove and Sort" for all code examples.

Visual Studio Tips
.NET