Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

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 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.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.