Code inspection: Convert local variable or field into constant (non-private accessibility)
This inspection reports public fields that always have the same compile-time value and can be turned into a const.
It is especially useful for fields, because global analysis can verify that the field is never written to and can safely update usages.
Example
public class Settings
{
public static readonly int DefaultPort = 8080;
}
public class Client
{
public int GetPort() => Settings.DefaultPort;
}
public class Settings
{
public const int DefaultPort = 8080;
}
public class Client
{
public int GetPort() => Settings.DefaultPort;
}
Quick-fix
The quick-fix converts the field to const. When required, the quick-fix also updates references so they use the constant correctly, for example replacing instance-qualified access with type-qualified access.
27 March 2026