All rules
CA1805Performance Enabled by default: No

Do not initialize unnecessarily

Do not initialize unnecessarily

Microsoft docs

Description

The .NET runtime initializes all fields of reference types to their default values before running the constructor. In most cases, explicitly initializing a field to its default value in a constructor is redundant, adding maintenance costs and potentially degrading performance (such as with increased assembly size), and the explicit initialization can be removed.

Cause

A field of a class is explicitly initialized to the default value of that field's type.

How to fix violations

In most cases, the proper fix is to delete the unnecessary initialization.

In some cases, deleting the initialization may result in subsequent CS0649 warnings being issued due to the field retaining its default value forever. In such cases, a better fix may be to delete the field entirely or replace it with a property:

Example

class C
{
    // Violation
    int _value1 = 0;

    // Fixed
    int _value1;
}

When to suppress

It is always safe to suppress the warning, as the warning simply highlights potentially unnecessary code and work that may be avoided.

Group results
0 yes 0 no
ConsensusNone (disabled)
Severity preference (yes voters)
Suggestion0
Warning0
Error0