Step by Step implement StyleCop custom rules with Visual Studio – Part 1

Instead of starting with an introduction of StyleCop, I would like you to read “Why should we use StyleCop?“. There is enough help on how to integrate StyleCop with Visual Studio, one can be found here. This article is focused on step by step implementation to add custom rules that we used in our project.

Install StyleCop : (Version 4.7)

Create a sample console project using Visual Studio. The idea here is to create a code base to test our custom rule. Code sample is as seen below :

using System;

namespace TestSampleStyleCopCustomRule
    class Program
        static void Main(string[] args)
              ("Vikram", "Shetty"));
        public static string
             (string firstWord,
              string secondWord)
               return firstWord + " "
                + secondWord;

Edit the .csproj you just created above and add the following line after last <Import Project=

  <Import Project="$(ProgramFiles)\MSBuild\StyleCop\v4.7\StyleCop.Targets" />

To treat StyleCop Warnings, as errors add the following line as a child of  PropertyGroup tag. Make sure you place it in PropertyGroup with Configuration|Platform=Debug. Mostly it will be on top as a first group:


Now once you build your solution you should get around 15 errors, The number of errors is not important here, just make a note of it because we will add a custom error in our Part 2 post, thus making sure our new custom rule works. If you want to make it more challenging fix all your errors.

Read next post of this step by step series here.


One thought on “Step by Step implement StyleCop custom rules with Visual Studio – Part 1

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s