Problem solve Get help with specific problems with your technologies, process and projects.

Let Microsoft StyleCop tame your wild C#

StyleCop 4.3 is a means to analyze C# source code to enforce a set of style and consistency rules.

The SDK documentation for StyleCop 4.3 recently became available for download on the MSDN Web pages. StyleCop analyzes C# source code to enforce a set of style and consistency rules, according to author and StyleCop creator Jason Allor. He has provided a detailed discussion on "How to integrate the StyleCop tool into an MSBuild based build environment." In fact, StyleCop can be run from inside of Visual Studio or integrated into an MS Build project.

To be more specific, StyleCop is really a rules-definition and processing environment that can apply what's in its rules base to C# source code to which it is applied. To be sure, you will also find a sizable pre-defined set of rules for C# from Allor and others that are ready to be put to work, including:

›Enforce a sort order on C# directives
›Require standard summary text for constructors and destructors
›Require explicit parentheses in arithmetic and comparison expressions to add clarity
›Require String.Empty rather than using the empty "" expression
›Require explanation message text in debug asserts
›Require justification text in Code Analysis suppressions
›Enforce use of built-in type aliases for int, string, float, and so forth
›Require a blank line between elements
›Disallos blank lines at the top or bottom of documentation headers
›Disallow empty static constructors; and
›Disallow empty unsafe, lock, checked, and unchecked statements.

In addition, StyleCop provides facilities to allow developers to define and apply their own style rules, and to manage code presentation and layout. StyleCop is NOT part of the Visual Studio Code Analysis suite, and its focus is on code style and consistency rather than syntax and semantics.

The SDK documentation is where you'll find the extensibility interface for StyleCop defines, along with examples of custom rules. These documents also explain how to plug StyleCop into the processing chain for command-line based build tools of various kinds. And finally, you can download a compiled help file (.chm) that documents all the default rules built into StyleCop, to help you understand in detail what's already available, and what kinds of changes or additions you might wish to make.

Anybody who's working with C# will probably find StyleCop of at least academic interest, if not an immediate candidate for their development toolset. This goes double for those building C# constructions inside Visual Studio.

Related StyleCop Info
StyleCop SDK documentation - StyleCop SDK documentation - Code.MSDN
How to integrate the StyleCop tool into an MSBuild based build environment - MSDN

Ed Tittel is a writer and trainer whose interests include XML and development topics, along with IT Certification and information security.

This was last published in September 2008

Start the conversation

Send me notifications when other members comment.

By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Please create a username to comment.