how to detect unused imports? - visual-studio

In my code im often adding some import statements like e.g.:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Threading;
During development some of these import statements may become unnecessary because i move the code that required these imports. In eclipse, unused imports are marked by the IDE, but in Visual Studio 2010 I couldn't find any hint. How can i detected them?

Visual Studio has this feature built in if you want to remove them rather than just detect them: Automatic Code Generation -> Organize 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.
Edit to include information from comments
Franci has chimed in with the Powershell extension to VS 2010 which adds in more functionality to do this task as well as others: http://visualstudiogallery.msdn.microsoft.com/en-us/e5f41ad9-4edc-4912-bca3-91147db95b99

Visual Studio 2019 comes with default code cleanup that fixes this. Shortcut is to press CTRL+K and CTRL+E as default.
https://learn.microsoft.com/en-us/visualstudio/ide/code-styles-and-code-cleanup?view=vs-2019
You can also use the command "Remove and Sort Usings" with CTRL+R and CTRL+G.

Related

Remove automatic addition of using statements on Visual Studio on using methods not in the current namespace [duplicate]

I am using Visual Studio with Unity. When I copy and paste a code in Visual Studio. It automatically adds an unnecessary namespace on the top. It is pretty annoying to see if there have many unused namespaces over time. May I ask if is this a bug or how can I turn off the auto import namespace when copy paste?
From the suggested name, I think Visual Studio confuses the "length" and "Length"
Here are the steps to reproduce the problem
On the top of the page in Visual Studio, go to Tools > Options > Text Editor > C# > Advanced > Uncheck the option for "Add missing using directives on paste" then click OK on the bottom of the page. And then restart Visual Studio.
Might be useful to some to note that tab completion will still auto-add missing using directives.
Here is an example with the option checked (the example specifics are not relevant but in this case an attribute is being pasted to an ASP.Net Core Razor Page):
Here is an example with the option unchecked:

Organize Usings in Visual Studio 2013 doesn't match StyleCop Rules

The organize usings option in Visual Studio 2013 doesn't match the style cop rule SA1208. For example:
The Organize usings would put them in:
Namespace.Entities;
Namespace.Interfaces;
System;
System.Linq;
But SA108 requires the system ones first.
System;
System.Linq;
Namespace.Entities;
Namespace.Interfaces;
Is there a way to make visual studio behavior match the StyleCop rule?
By default, my Visual Studio installation already sorts System usings first.
But you can change it. See here (check the note).
Go to Tools -> Options -> C# (I assume you are working on a C# project) -> Advanced. Then check the box saying "Place 'System' directives first when sorting usings"
EDIT:
For recent versions, MS moved the documentation to a different page: see here (check the Organize usings section).

Add custom code snippet to visual studio intellisense

There is this nice program that enable you to easily create code snippets. I already managed to create snippets but it will be nice if the snippet where to show up in visual studio's intellisense.
For example visual studio already has several built in code snippets such as the one for creating the constructor of a class:
note that it was really easy and fast to use it.
On the other hand when I create my custom snippet with the program that I provided on the first link, these are the steps that I have to do in order to use it:
on step 3 I have to select the folder where the snippet that I created is located then on step 4 locate it.
It will be nice if I could use the code snippet that I created just like the ones that visual studio provides like the constructor one that I showed on the first image. Maybe if I place the snippet that I just created and place it where visual studio store the built in onces it works.
I had to add a shortcut to the snippet.
after adding the shortcut it appears on the intellisense without having to navigate to the folder where it was located by pressing ctrl+k and ctrl+x

How to insert code snippet for Debug.WriteLine(); in Visual Studio?

I can simply insert code snippet for the Console.WriteLine(); by just using the cw with Tab + Tab. But I'm not able to find the same option for the Debug.WriteLine();.
I want to know how can we customize the code snippet template in Visual Studio for Windows Phone?
In Visual Studio 2010 (and assuming other versions as well), there is not a snippet for Debug.WriteLine(). In that case, you'll need to create a custom snippet. Fortunately, it isn't that hard with the available resources:
MSDN: Creating and Using IntelliSense Code Snippets
The Snippet Editor
Snippet Designer (Visual Studio Extension) and the related CodePlex Site
If you're using ReSharper, then Code Templates and specifically Live Templates are a quick and easy option.

Visual Studio Intellisense does not provide the 'using namespace' option

When I press Ctrl+. over any symbol in the editor, it has stopped showing me the option of inserting a using statement to include the namespace on the top of my file.
It just shows me the option to use the namespace qualified type name as depicted in the snapshot.
I am using Visual Studio Community 2015 RC. I have restarted it several times but that didn't help.

Resources