Cleaning Up "Using" Directives In A File In C#

We use "Using" directives at the top of a file to bring various namespaces into scope so that we don’t have to fully qualify the named members within those namespaces.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Drawing;  
  4. using System.Linq;  
  5. using System.Text;  

The 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 the Using directives for namespaces that you don’t use.

To remove unused namespaces, you can right-click on the Source Code Editor and select "Organize Usings" >> "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

Ebook Download
View all
Learn
View all