Removing Unused Usings

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 OrganizeUsings, and then click Remove Unused Usings.

    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.

Remarks

Conditional Pre-processor Directives

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.

Comments

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.

See Also

Concepts

Organizing Using Statements

Sort Usings

Reference

Advanced, C#, Text Editor, Options Dialog Box

using Directive (C# Reference)

extern alias (C# Reference)