Stylecop Visual Studio 2019

  



NOTE: This project is no longer very active. See the 'Considerations' section below.

StyleCop analyzes C# source code to enforce a set of style and consistency rules. It is available in two primary forms:

  1. The StyleCop Visual Studio extension, which allows StyleCop analysis to be run on any file, project, or solution in Visual Studio without modifying the source code. Visual Studio 2010, 2012, 2013, 2015, 2017, and 2019 are supported by this extension.
  2. The StyleCop.MSBuild NuGet package, which allows StyleCop analysis to be added to any .NET 4.0+ project without installing anything else on the system.
  • Place ‘System’ directives first when sorting usings. Going forward, whenever you will apply “Sort Usings”, or “Remove and Sort”, Visual Studio will sort the usings as usual, but would also ensure that, all the system directive places in the top in a sorted order.
  • The NuGet package continues with the installation and shows Successfully installed message on the Output window of Visual Studio. How To Install StyleCop Analyzer In Visual Studio.

There is also a ReSharper plugin that can be added using ReSharper's Extension Manager.

StyleCop used to be a Visual Studio plugin and a NuGet package. You can still use this in Visual Studio 2019, but the current recommended way to use StyleCop is to use the Roslyn-based analyzers. The next time you start Visual Studio the squiggly lines marking violations of the StyleCop rules are gone – but just for those projects you don’t want it. A small line of configuration and StyleCop behaves as you want it. It’s much less work than to remove StyleCop altogether and much less annoying than to ignore the squiggly. Visual Studio 2010, 2012, 2013, 2015, 2017, and 2019 are supported by this extension. The StyleCop.MSBuild NuGet package, which allows StyleCop analysis to be added to any.NET 4.0+ project without installing anything else on the system. There is also a ReSharper plugin that can be added using ReSharper's Extension Manager.

Considerations

While pull requests will continue to be accepted, it is unlikely that any major development (including support for newer C# syntax) will be done on this project. It is increasingly difficult and inefficient to maintain the custom C# parser used by StyleCop. The primary motivation for recent maintenance work was to allow developers who were already using StyleCop to upgrade to Visual Studio 2015 and C# 6.

The Roslyn-based StyleCopAnalyzers project is strongly recommended for developers who use only Visual Studio 2015 or later.

All we developers would have spent time in cleaning up the code after we are done with a long day of code and coffee! for .Net developers it is to do with removing all the using clause added automatically by visual studio/nuget that you no longer need, removing variables that was not put to use, adding read only to eligible private variables, adding or removing braces from single statement blocks etc etc.

There are a list of tools/extensions that helped developers to automate most of these tasks. Popular of these tools were Re-Sharper, StyleCop, CodeMaid...

Stylecop 4.7 Visual Studio 2019

Microsoft with its visual studio versions starting 2012 incorporated many of these feature into the IDE. Up until Visual studio 2017, developers were able to remove unused using, sort using clauses and other cleanups by right-clicking the file and selecting its option or choosing refactoring quick action bulb.

Executing Code cleanup from visual studio

Visual Studio 2019 has taken this to the next level by introducing an option to run a predefined set of code clean up actions on an individual file or on all files in a project/solution and this option is available in visual studio status bar, Analyse Menu or on your right-click context menu of file/project/solution.

Stylecop install

Code cleanup profiles

Stylecop Visual Studio 2019

As you can see from the above screenshot visual studio 2019 allows you to create a list of code clean up activities (which they call fixers) and save them as what they call a profile (no, you cannot define a name for the profile and there is currently option for only 2 profiles). You can choose from a list of predefined fixers (currently 14 of them are present). To configure profile, you can choose the option from any of the menu option in the above screenshot or you can choose Visual Studio menu Analyse > Code cleanup > Configure code cleanup
and you get a window like below to choose the fixers into profiles

Stylecop Visual Studio 2019 Full

It is just the features first release and it something every developer will appreciate and it has a potential to do much more. Visual studio developer community has already started pouring in suggestions and feedback on the feature to make it better.