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, advising how to improve the code in the project template for a console app when targeting .NET 5.0 so that the console app already has a Program
class with a Main
method:
- Use your preferred code editor to add a console app project, as defined in the following list:
- Project template: Console Application /
console -f net5.0
- Workspace/solution file and folder:
Chapter06
- Project file and folder:
CodeAnalyzing
- Target framework: .NET 5.0 (Current)
- Project template: Console Application /
- 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"...