Photo by

Use StyleCop to improve your code quality

Improve your code quality easily!

Posted by Damien Aicheh on 06/20/2019 · 5 mins

When you develop a project you probably work within a team. Each developer has his own programming habits. To avoid having a code that looks different in each class we need to find a way to harmonize it.

Introducing StyleCop

StyleCop is a C# source code analyzer that allows you to enforce a set of style and consistency rules. You can adapt the rules that you don’t want to check depending on your needs. This kind of tools helps you to have a code:

  • Readable
  • Maintainable
  • Easy to follow for new developers

To add StyleCop to your project you have different possibilities:

  • Using the StyleCop.MSBuild Nuget
  • Using the Visual Studio extension

I will focus this tutorial on the first solution, the advantages of this one are:

  • You keep your StyleCop configuration with your project
  • If a new developer arrive in your team, he won’t need to install the Visual Studio extension
  • When you get the project, you just need to restore the Nugets and you are good to go

Setup StyleCop

First of all add the StyleCop.MSBuild Nuget package to the project you want to analyse.

Now, for example let’s create a new class called MyViewModel.cs here is what is basically generated:

using System;
namespace StyleCopDemo
    public class MyViewModel
        public MyViewModel()

If you compile your project you will immediately see a lot of warnings in this class:

ViewModel warnings

Configure StyleCop

Let’s add a new file called Settings.StyleCop to your project, this one will help you to configure and adjust the rules you want or not in your project.

Let’s add a default configuration:

<StyleCopSettings Version="4.3">
    <StringProperty Name="MergeSettingsFiles">NoMerge</StringProperty>
    <StringProperty Name="Culture">en-US</StringProperty>
    <CollectionProperty Name="RecognizedWords">
    <Parser ParserId="StyleCop.CSharp.CsParser">
        <BooleanProperty Name="AnalyzeDesignerFiles">False</BooleanProperty>
    <Analyzer AnalyzerId="StyleCop.CSharp.DocumentationRules">
        <StringProperty Name="CompanyName"></StringProperty>
        <StringProperty Name="Copyright">MS-PL</StringProperty>
        <BooleanProperty Name="IgnorePrivates">True</BooleanProperty>
        <BooleanProperty Name="IgnoreInternals">True</BooleanProperty>
        <BooleanProperty Name="IncludeFields">False</BooleanProperty>
    <Analyzer AnalyzerId="StyleCop.CSharp.NamingRules">
        <CollectionProperty Name="Hungarian">
    <Analyzer AnalyzerId="StyleCop.CSharp.OrderingRules">
        <BooleanProperty Name="GeneratedCodeElementOrder">False</BooleanProperty>

Depending on what you want and what you need you can disable some rules. Let’s take an example, if you look at the screenshot above we can see that StyleCop is complaining for different rules. The rule SA1633 correspond to this warning message:

SA1633 : CSharp.Documentation : The file has no header, the header Xml is invalid, or the header is not located at the top of the file.

If you want to disable this rule you just need to:

  • Go to the StyleCop repository
  • Do a search with the rules identifier, in our case it’s SA1633
  • Find the associated rules name, in our case it’s FileMustHaveHeader

Then you can disable it by specifying it inside our Rules tags:

<Rule Name="FileMustHaveHeader">
    <BooleanProperty Name="Enabled">False</BooleanProperty>

Now if you recompile, the warning will disappear. It’s time for you to experiment it in your projects! I created a demo project just to show you a simple example.

You will find full source code in this Github repository.

Happy coding !

You liked this tutorial ? Leave a star in the associated Github repository!

Do not hesitate to follow me on to not miss my next tutorial!