Field can be made read-only

CodeRush Classic shows the Field can be made read-only code issue if a field is initialized inside its declaration or (and) a value is assigned to this field only within constructors.

Fix

Add the readonly keyword to the field declaration statement.

Purpose

CodeRush Classic suggests you make fields read-only, because the use of read-only fields makes your code safer.

Example

public class MyClass
{
    //constructors
    public MyClass(){ }
    public MyClass(string text)
    {
        _Text = text;
    }
    public MyClass(object obj)
    {
        _Text = obj.ToString();
    }
    //fields
    private string _Text = "MyText";
    //properties
    public string Text
    {
        get { return _Text; }
    }
}

Fix:

public class MyClass
{
    //constructors
    public MyClass(){ }
    public MyClass(string text)
    {
        _Text = text;
    }
    public MyClass(object obj)
    {
        _Text = obj.ToString();
    }
    //fields
    private readonly string _Text = "MyText";
    //properties
    public string Text
    {
        get { return _Text; }
    }
}