Search code examples
c#visual-studio-2019resharperstylecop

Some questions about StyleCop


I am using both ReSharper and StyleCop extentions on VS 2019 and I have some question.

  1. What is the difference between using StyleCop ReSharper extension and using StyleCop VS extension?
  2. Is there any way to apply the same edited Settings.StyleCop for all C# project I will use without manually copy it into project folder?

Solution

  • I would recommend not using either the ReSharper or Visual Studio extensions for StyleCop. The preferred way of using static code analysis (especially for .net 5 onwards) is to use the NuGet packages. There are several you can try:

    • StyleCop.Analyzers
    • SonarAnalyzer.CSharp
    • Roslynator.Analyzers

    These can all be configured using the .editorconfig file. All their rules will be under Project->Dependancies->Analyzers in Visual Studio's Solution Explorer where you can set their severity.

    The advantage of using the NuGet packages is that you can apply them to just the projects you want. As they are now part of your build then they will run on a build server and can be configured to fail the build if rules aren't adhered to.

    The ReSharper and Visual Studio extensions only apply to the individual user who has them installed (each possibly having their own rules).

    You can create a global .editorconfig file to apply to all of your projects. This works in the same way as the older Settings.StyleCop file where it would search up your folder branch till it found the file.

    Lots of Microsoft Docs:

    EditorConfig settings

    Code analysys using Roslyn

    Analyzers vs StyleCop

    You may also find that the ReSharper and/or Visual Studio extensions may no longer be supported and may not work as expected with later versions of .net/C#.