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).
Related
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:
I want to extract comments from code files in a Visual Studio extension. Is there a way to do it using Visual Studio Automation without having to parse code text myself?
PS: Roslyn is not a choice, because I'm not restricted to C# and VB.NET only.
Various code elements like CodeClass and CodeFunction have the Comment property that return the header comment, accessible using Visual Studio code model.
And if a document is opened in VS editor, you can check SnapshotSpan classifications for PredefinedClassificationTypeNames.Comment.
I am using Microsoft Visual Studio Premium 2012.
Microsoft directions states:
Main Menu - On the Edit menu, point to IntelliSense, point to Organize Usings, and then click Remove Unused Usings.
However, there is no option called "Organize Usings" in the "Intelliense" menu on the "Edit" menu-tab. I can use the context-menu to accomplish this...but that option only appears for a single open code-behind file (when I have it open). Doing so one code-file at-a-time is useless to me in a large project.
Also...
I have looked at other questions for previous version of Visual Studio on this site, and their answers don't work in 2012 Premium.
So my question is...
How can I "Remove Unused Usings" within Microsoft Visual Studio Premium 2012 across a single project or solution?
You would probably have to write a Visual Studio Extension to do this.
You can use the Macro at this link that opens all files of your project and remove unused usings. Otherwise PowerCommands contains some useful extensions, including one that remove all unused usings (never tested it though).
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.
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.