Using an analyzer to write better code
.NET analyzers find potential issues and suggest fixes for them. StyleCop is a commonly used analyzer for helping you write better C# code.
Let’s see it in action:
- Use your preferred code editor to create a Console App/
console
project namedCodeAnalyzing
in aChapter01
solution/workspace. - In the
CodeAnalyzing
project, add a package reference forStyleCop.Analyzers
. - Add a JSON file to your project named
stylecop.json
for controlling StyleCop settings. - Modify its contents, as shown in the following markup:
{ "$schema": "https://raw.githubusercontent.com/DotNetAnalyzers/StyleCopAnalyzers/master/StyleCop.Analyzers/StyleCop.Analyzers/Settings/stylecop.schema.json", "settings": { } }
The
$schema
entry enables IntelliSense while editing thestylecop.json
file in your code editor.
- Move the insertion point inside the
settings
section and press...