The requested page is not available for the requested platform. You are viewing the content for Default platform.

Declaration can be a constant

CodeRush Classic shows the Declaration can be a constant code issue if a field or a variable is initialized inside its declaration, provided that its value is never changed.

Fix

Add the const keyword to the field or variable declaration statement.

Purpose

CodeRush Classic suggests you convert the fields and variables, whose value is never changed to a constant, because the use of constants makes your code safer.

Example

public class MyClass
{
    public MyClass()
    {
        Name = _DefaultName;
    }
    public MyClass(string name)
    {
        Name = name;
    }
    public string Name { get; private set; }
    private string _DefaultName = "Default";
}

Fix:

public class MyClass
{
    public MyClass()
    {
        Name = _DefaultName;
    }
    public MyClass(string name)
    {
        Name = name;
    }
    public string Name { get; private set; }
    private const string _DefaultName = "Default";
}