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/
consoleproject namedCodeAnalyzingin aChapter01solution/workspace. - In the
CodeAnalyzingproject, add a package reference forStyleCop.Analyzers. - Add a JSON file to your project named
stylecop.jsonfor 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
$schemaentry enables IntelliSense while editing thestylecop.jsonfile in your code editor.
- Move the insertion point inside the
settingssection and press...