#1,185 – Managing using Directives, part II

You can add missing using directives by using the Resolve command.  You can also clean up the current list of using directives in a file, removing the ones that are no longer needed.

You can remove unneeded using directives by clicking anywhere within a file and selecting Organize Usings | Remove Unused Usings.

In the example below, we start out with 20 using directives at the top of the file.

1159-001

We then select Remove Unused Usings.

1159-002

After we execute this command, we’re left with only four using directives at the top of the file.

1159-003

#758 – Cleaning Up using Directives in a File

You use using directives at the top of a file to bring various namespaces into scope, so that you don’t need to fully qualify named members within those namespaces.

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

Extra using directives don’t hurt anything–the list of directives is just a list of possible namespaces to consult when resolving a name.  Your code may not actually reference members in all of the listed namespaces.

For cosmetic purposes, however, there is an easy command in Visual Studio that you can use to clean up using directives for namespaces that you don’t use.

To remove unused namespaces, you can right-click in the source code editor and select Organize Usings and then Remove Unused Usings.  After you do this, you’ll be left only with namespaces that contain members that your code contains.

758-001

 

758-002