DEV Community

Cover image for C# Linting and Formatting Tools in 2021

C# Linting and Formatting Tools in 2021

Sam Magura on October 03, 2021

tl;dr: Use SonarLint and optionally StyleCop. The JavaScript ecosystem has amazing tools for formatting and statically analyzing your code: Pretti...
Collapse
 
valentinepalazkov profile image
Valentine Palazkov • Edited

Thank you for the research - it's very useful.

It worth to mention that ReSharper costs $299 for organizations.

As for me, I have dotUltimate personal subscription that costs me $89/year and has much more than just ReSharper - it includes pretty much everything that C# developer would need.

But you are right that ReSharper is worthless for a project more than 300K lines of code because of performance issues.

I checked VS2022 + ReSharper - it's much better, I would say.

My guess that it's because VS2022 is a 64bit process.

But anyway, I switched to JetBrains Rider almost 4 years ago and never looked back.

Collapse
 
shadowcs profile image
Honza Rameš • Edited

Great article, thanks.

However I'd advice against using rulesets at this time and migrate to .editorconfigs which offer more options (but you'll loose the GUI for editing rulesets - which have its quirks for .NET Core projects anyway). See docs.microsoft.com/en-us/visualstu.... You can port between the two, see github.com/dotnet/roslyn/issues/41393.

Also there are two more analyzers worth mentioning: Roslynator and FxCop. FxCop is now part of the .NET SDK (since 5.0) and renamed to .NET analyzers. Several rules have been discontinued and a lot of rules (or all, depends on TargetFramework) are disabled by default. See docs.microsoft.com/en-us/dotnet/fu....

Collapse
 
srmagura profile image
Sam Magura

Thanks for bringing this up Honza. I did not know you could configure rules via .editorconfig. I'll try to get that working and then update the post accordingly.

Reference on how to use .editorconfig to configure rules: docs.microsoft.com/en-us/visualstu...

Collapse
 
binarypatrick profile image
BinaryPatrick

Love this post, thank you for all the research. Have you looked into any solutions adding this to a build pipeline or check in process? I'd really love to have an automated task in the PR to run this and commit styles changes to keep everything correct.

Collapse
 
srmagura profile image
Sam Magura

Thanks! The Johnny Reilly post I linked shows how to automate dotnet-format with a precommit hook. I don't think it will be possible to automate any of the other tools to the same extent.

Since SonarLint and StyleCop generate build warnings, you will see those warnings in CI. Here's a screenshot of a Sonar warning from my Azure Pipelines build:

Pipelines

Collapse
 
belav profile image
Bela VanderVoort • Edited

If you want an opinionated formatter for c# you should check out github.com/belav/csharpier, it is coming along nicely. The formatting is fairly stable at this point, I am mostly just dealing with edge cases.

Collapse
 
twofingerrightclick profile image
twofingerrightclick

This is definitely my favorite tool. Hands off. Makes for those who do a lot of Typescript.

Collapse
 
tomasforsman profile image
Tomas Forsman • Edited

Best post on the subjekt I've come across. Very well put together! It's highly appreciated.

Collapse
 
srmagura profile image
Sam Magura

Thank you!

Collapse
 
johnnyreilly profile image
John Reilly

Thanks for the link to my Prettier post Sam! I've since written a follow up more specifically on linting as I've been digging into the wonderful world of Roslyn Analyzers. blog.johnnyreilly.com/2022/04/06/e...

Collapse
 
ohritz profile image
Sohan Fernando

Just to add another tool to the mix.
jetbrains.com/help/resharper/ReSha...
is free.
We are just starting to try it out, to see if we can do some style cleanup in bulk on older codebases.

Collapse
 
aliki212 profile image
AlikiP

Great article!Thank you for the information and documentation!I am still learning so I'll stick with Resharper for now, good to know SonarLint for the future, cheers!

Collapse
 
ptkhuong96 profile image
PTKhuong96

Thanks. it's great

Collapse
 
alirezanet profile image
AliReZa Sabouri • Edited

Hi, I think you should combine these with husky.net, check it out
github.com/alirezanet/Husky.Net

Collapse
 
guibranco profile image
Guilherme Branco Stracini

CSharpier is another great tool! csharpier.com/docs/About

Collapse
 
h_assefi profile image
Hossein Assefi • Edited

Hi Sam
The article is very helpful. But, I wonder if any of these tools can be used in CI pipeline?

@srmagura

Collapse
 
mcm profile image
Michael McDonald

Great summary thanks for the article. Just one more to add to the list csharpier.com it's similar to dotnet format except does the things you highlighted are missing.

Collapse
 
danielcumings profile image
Daniel Cumings

Rider is worth checking out. Most of the developers at my company have switched to rider. The only thing we still need VS for is fixing binding redirects in old net framework code.