StyleCop Tutorial
StyleCop is a source analysis tool for C#. It can be used for analysing source code as opposed to compiled assemblies which is the area for FxCop. StyleCop is currently in version 4.2 and can be downloaded here. In this tutorial I will show you how to use StyleCop.
I will create a simple console application and use StyleCop to analyse the source. This killer app just initialises a class and prints out few properties. Below is the code for the application.
public class Customer
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Address { get; set; }
public CustomerStatus Status { get; set; }
public override string ToString()
{
string result = string.Format
("First Name = {0}\nLast Name = {1}\nAddress = {2}\nStatus = {3}"
, FirstName, LastName, Address, Status);
return result;
}
}
public enum CustomerStatus
{
Active,
InActive
}
class Program
{
static void Main(string[] args)
{
Customer customer = new Customer
{
FirstName = "John",
LastName = "Smith",
Address = "1 George Street, Sydney",
Status = CustomerStatus.Active
};
Console.WriteLine(customer.ToString());
Console.ReadKey();
}
}
I can run source code analysis on the entire project via Tools menu or an individual file by right clicking on the class file.

By running source analysis on Program.cs I get 34 violations.

Depending on your style or your coding standards, you will either agree or disagree with detected violations. For example in the result above I do not agree with rule SA1200 “All using directives must be placed inside of the namespace”. I have never declared my using statements inside the namespace and I do not intend to do so in future. So what can I do? I can easily modify the rule set. Let’s see how we can do this.
By default StyleCop is installed at C:\Program Files\Microsoft Source Analysis Tool for C#. A quick glance over the contents of this folder look like this.

Notice the second last file which is Settings.SourceAnalysis. Double clicking it opens up the SourceAnalysisSettingsEditor where I can select/deselect the rules.

I can turn off rule SA1200 “All using directives must be placed inside of the namespace” and then if I run analysis again, my result set will not show it as a rule violation.

As you can see from screenshots above that rules are categorised into different categories such as Documentation Rules, Layout Rules, Naming Rules etc. This makes is easy work with rules in a logical way.
Conclusion
StyleCop is a light weight source code analysis tool which fills in the necessary gap. One of the benefits of StyleCop can be realised while conducting code reviews. Lately StyleCop has had its own share of controversy, but it is good that Microsoft is paying attention to this nifty tool which could attract a significant user base. StyleCop can be downloaded here.
13 Responses to StyleCop Tutorial
Leave a Reply Cancel reply
Top Posts
- LINQ To SQL Tutorial
- LINQ To SQL Join On Multiple Conditions
- Code Sample: Programmatically Download File Using C#
- Windows 7 Control Panel In Classic Mode
- More Details Emerge On Microsoft Master Certification
- Use SqlConnection With LINQ To SQL
- Free Icons And Images With Visual Studio 2008
- Capture XML In WCF Service
- Dynamic Sort With LINQ
- StyleCop Tutorial
Tags
.Net 2010 ADO.NET ASP.NET Azure Blogging Books Browsers C# Certification Cloud Computing Code Snippets Community Data Services Eclipse Entity Framework Google IDE Java LINQ Mac Microsoft Museum NetBeans Office Oracle REST SharePoint Silverlight SQL Server T-SQL Tips Tools Training Visual Studio Visual Studio 2010 WCF Web Windows Windows 7 Windows Forms Windows Live WMI WPF XAML



I also found it useful to disable “Analyze designer files”
I agree. Analysing designer files can be a nuisance.
Where is the VB.Net version!!! :-(((
Soon please!
Regards
Harry
My installation was in a different directory:
C:\Program Files\Microsoft StyleCop 4.3
How can I disable the SA1400 just for the static void Main() Entry point?
Mike,
I would imagine that you should be able to put an attribute on a method to disable a StyleCop rule. But this does not seem to be the case. Have a look at this link:
http://code.msdn.microsoft.com/sourceanalysis/WorkItem/View.aspx?WorkItemId=25
There is a comment from someone at Microsoft
“There is no plan now to allow this but we will leave the issue open for consideration. StyleCop rules are much more deterministic than FxCop and it is currently by design that suppressions are not allowed.”
I think ability to suppress a rule using attributes is much desirable but I guess we will have to wait for a while.
Hi Deepak,
Thank you. So now i know im not alone with this “problem”
[...] StyleCop Tutorial auf thereforesystems.com [...]
[...] version of StyleCop from MSDN including documentation and samples. If you need more information, here you can find a good tutorial on how to successfully integrate StyleCop in your .NET [...]
Hi! I was surfing and found your blog post… nice! I love your blog. :) Cheers! Sandra. R.
[...] StyleCop Tutorial [...]
Thanks for the article! :)
Develop a VB.NET version!! PLEASE!!