Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Method of removing unnecessary namespaces in .NET Applications?

Is there a way to remove unnecessary "using" statements from a class?

For example I might have a complex class in which I might add my own namespaces but there are also some namespaces that are added automatically by Visual Studio which I also might be using:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Windows.Navigation;
using System.IO.IsolatedStorage;
using Microsoft.Phone.Shell;
using System.Net.NetworkInformation;
using System.ServiceModel.Syndication;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Xml;
using System.Xml.Linq;
using Microsoft.Phone.Controls;
using Telerik.Windows.Controls;
using MyApp.Models;
using MyApp.Services;

Is there a way to detect which namespaces are being used and which ones aren't so they can be removed?

NOTE: I believe this question would apply to any platform (Desktop, Phone, Web, etc).

like image 481
Edward Avatar asked May 18 '11 18:05

Edward


1 Answers

Yes - Visual Studio can do this for you. In the context menu in the text editor, choose Organize Usings > Remove and Sort. (You can just remove, but why not sort at the same time? :)

Personally I have it on a hotkey of Ctrl-Shift-U... it's a very handy feature.

As a side note, these aren't using statements - they're using directives. using statements are the ones which dispose of resources.

like image 180
Jon Skeet Avatar answered Oct 26 '22 23:10

Jon Skeet