C# – How to treat warnings like errors

Warnings are easy to ignore and forget about, which isn’t good. They point out potential problems that you might want to fix. To make it easier to pay attention to warnings, you can treat them like errors. You can choose which warnings to treat like errors by using settings in the project file (or in … Read more

CA2208: Instantiate argument exceptions correctly

The CA2208 code analysis rule checks for common mistakes when constructing argument exceptions. There are three main argument exception classes: ArgumentException, ArgumentNullException, and ArgumentOutOfRangeException. Unfortunately, it’s easy to make a mistake when using these. I’ll explain the common mistakes that CA2208 checks for and how to fix them (and when to suppress the warning instead). … Read more

C# – IDE0060: Remove unused parameter

If you have a method with a parameter, and that parameter is not used in the method, then you’ll get the IDE0060 message telling you to remove the unused parameter. Here’s an example of code that would trigger this message: The encrypt parameter isn’t being used in the Send() method, triggering the IDE0060 message: IDE0060 … Read more

CA1062: Validate parameter is non-null before using it

When you have a public method that isn’t null checking its parameters, then you’ll get the CA1062 code analysis warning. This is part of the Nullable Reference Types feature. For example, the following code isn’t null checking the movieRepository parameter: This results in the CA1062 code analysis warning: CA1062 In externally visible method ‘void StreamingService.LogMovies(MovieRepository … Read more