C#

C#

Made by DeepSource

Field that was initialized inline is being overwritten in the constructor CS-W1072

Bug risk
Major

There are different ways of initializing a field. You can either initialize it inline or set an appropriate value in the constructor. Doing both results in loss of value as the constructor overwrites any previously assigned value. It is therefore recommended that you choose any one of the appropriate ways of initializing a field.

Bad Practice

class C
{
    private int v = 1;

    public C()
    {
        // ...
        v = 2; // Previous value `1` is lost!
    }
}

Recommended

class C
{
    private int v;

    public C()
    {
        // ...
        v = 2; // No longer initialized inline
    }
}