Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

AV0017: Remove unnecessary default value

Value
Rule IDAV0017
CategoryUsage
Fix isNon-breaking

Cause

An option is assigned the value it already has.

Rule Description

Most options already hold a usable value before any configuration runs. Assigning that same value restates the default without changing anything.

Consider the following code:

builder.Services.AddApiVersioning(
    options =>
    {
        options.ReportApiVersions = false;
        options.RouteConstraintName = "apiVersion";
    } );

Both assignments state what the options already hold.

The default of a property is matched by the type declaring it rather than by name alone, because the same name can carry a different default on a different set of options. The default API version is reported by AV0011 instead, because it can be spelled more than one way, and a value the API explorer takes from the API versioning options is reported by AV0024.

How to Fix Violations

Remove the assignment.

builder.Services.AddApiVersioning();

When to Suppress Warnings

It is safe to suppress this rule if you prefer options to be stated explicitly so that a future change to a default is deliberate rather than inherited.