Muokkaa

Jaa


CA2245: Do not assign a property to itself

Property Value
Rule ID CA2245
Title Do not assign a property to itself
Category Usage
Fix is breaking or non-breaking Non-breaking
Enabled by default in .NET 9 As suggestion

Cause

A property was accidentally assigned to itself.

Rule description

C# compiler generates a warning CS1717: Assignment made to same variable; did you mean to assign something else? when a field, local or parameter symbol is assigned to itself. Such a mistake is common when a local, parameter, or field symbol has a name similar to another symbol in scope. Instead of using different symbols on the left-hand and right-hand side of the assignment, the same symbol was used on both sides. This leads to a redundant assignment of the value to itself and generally indicates a functional bug.

Assigning a property to itself is also a similar functional bug for almost all real world cases. However, in some extreme corner cases, fetching a property value can have side effects and the property's new value is different from the original value. If so, property self-assignment is not redundant and cannot be removed. This prevents the compiler from generating a CS1717 warning for property self-assignment, without introducing a breaking change for these cases.

Rule CA2245 aims at filling this gap. It reports the violation for property self-assignment to help fix these functional bugs. For the small set of corner cases where property self-assignment is desirable, CA2245 violations can be suppressed in source with an appropriate justification comment.

How to fix violations

To fix violations, use different symbols on the left-hand and the right-hand side of the assignment. For example, the following code snippet shows a violation of the rule and how to fix it:

public class C
{
    private int p = 0;
    public int P { get; private set; }

    public void M(int p)
    {
        // CS1717: Accidentally assigned the parameter 'p' to itself.
        p = p;

        // CA2245: Accidentally assigned the property 'P' to itself.
        P = P;
    }
}
public class C
{
    private int p = 0;
    public int P { get; private set; }

    public void M(int p)
    {
        // No violation, now the parameter is assigned to the field.
        this.p = p;

        // No violation, now the parameter is assigned to the property.
        P = p;
    }
}

When to suppress warnings

It is safe to suppress violations from this rule if fetching a property value can have side effects and the property's new value is different from the original value. If so, property self-assignment is not redundant. A justification comment should be added to the suppression to document this as expected behavior.

Suppress a warning

If you just want to suppress a single violation, add preprocessor directives to your source file to disable and then re-enable the rule.

#pragma warning disable CA2245
// The code that's violating the rule is on this line.
#pragma warning restore CA2245

To disable the rule for a file, folder, or project, set its severity to none in the configuration file.

[*.{cs,vb}]
dotnet_diagnostic.CA2245.severity = none

For more information, see How to suppress code analysis warnings.

See also