

So, how can we get recommendations or best practices? The answer to that question is to enable Code Analysis and then configure your desired ruleset. With that viewpoint, warnings are still essentially only pointing out significant faults in your codebase they aren’t pointing out suggestions or even best practices. Now, let’s continue with the viewpoint that compiler warnings are errors, just left as warnings because the compiler team was worried about upgrades to new compiler versions and backward compatibility. Hence, the rationale in the previous section for always enabling warnings as errors. But in general, I like to consider new compiler warnings as errors the compiler team wished they could have written. So, most of the time, compiler teams will only add recent warnings to a compiler, and by default, those warnings can be disregarded.

Most compiler teams are wary of adding new errors because it will discourage upgrades to a new compiler version. Compiler warnings are provided typically because the compiler may be a deciding factor, but isn’t sure of your intention, hence a warning that they hope you will read and then fix. Compiler errors are typically there because you made a syntax error, and the compiler can’t generate any code. Why?: Let’s talk about compiler warnings and errors first. You can then add the following code inside the top-level tag: You can reference the screenshot below.įor those of you on Linux/MacOS X or using VSCode, you can instead open the. On the Build tab, be sure and use the drop-down for Configuration to select “All Configurations.” Then, under the Treat warnings as errors, select “All” for the radio button and save the project. On the properties screen, you can navigate to the Build tab. How?: You can right-click on a Project in Visual Studio and select “Properties…”. It keeps unfixed warnings from ever entering my codebase.

This is the single biggest thing I do when starting any C# project. It would be best if you considered asking developers to include comments explaining why the warning was disabled. In either case, you now have an affirmative decision in the codebase that can be traced back with version history. As a developer, you can still disable alerts at the entire project level, or you can #pragma directives to disable warnings for given cases in the codebase. By enabling warnings as errors, every compiler warning will now manifest as an error preventing compilation until addressed. Instead of relying on human discipline, why not merely automate that discipline by asking for the compiler’s assistance. However, even for some of the most diligent programmers on a project, they might have a bad day and miss a warning message from the compiler or forget to fix it before committing code to a project. Why?: You might think that having warnings is enough. Consider Enabling Nullable Reference Checking.NET and Visual Studio but may be configured differently. Most of the below practices are still recommended for earlier versions of. NET Core 3 and Visual Studio 2019 or later for each of the below practices. First, let me present my comprehensive list of practices, then we can dive into each one individually. These best practices are gleaned from years of experience developing C# solutions. and other tools you may consider acquiring, but those are out of scope for this blog post. Of course, there are other best practices you should follow, such as unit testing, code coverage, continuous integration, etc. I have scoped this blog post to only cover those best practices to those that can be enabled before you write a single line of code and utilize the built-in features of Visual Studio and VS Code. Let’s discuss some best practices you should employ or consider before writing that first line of code. You are about to start and new C# project and have opened Visual Studio.
