Gendarme.Rules.Performance.PreferLiteralOverInitOnlyFieldsRule Class
This rule looks for InitOnly fields (readonly in C#) that could be turned into Literal (const in C#) because their value is known at compile time. Literal fields don't need to be initialized (i.e. they don't force the compiler to add a static constructor to the type) resulting in less code and the value (not a reference to the field) will be directly used in the IL (which is OK if the field has internal visibility, but is often problematic if the field is visible outside the assembly).

See Also: PreferLiteralOverInitOnlyFieldsRule Members

Syntax

[Gendarme.Framework.EngineDependency(typeof(Gendarme.Framework.Engines.OpCodeEngine, Gendarme.Framework, Version=2.8.0.0, Culture=neutral, PublicKeyToken=null))]
[Gendarme.Framework.FxCopCompatibility("Microsoft.Performance", "CA1802:UseLiteralsWhereAppropriate")]
[Gendarme.Framework.Problem("Static readonly fields were found where a literal (const) field could be used.")]
[Gendarme.Framework.Solution("Replace the static readonly fields with const(ant) fields.")]
public class PreferLiteralOverInitOnlyFieldsRule : Gendarme.Framework.Rule, Gendarme.Framework.ITypeRule

Remarks

This rule is available since Gendarme 2.2

Example

Bad example:

Example

            public class ClassWithReadOnly {
            	static readonly int One = 1;
            }
            

Example

Good example:

Example

            public class ClassWithConst
            {
            	const int One = 1;
            }
            

Requirements

Namespace: Gendarme.Rules.Performance
Assembly: Gendarme.Rules.Performance (in Gendarme.Rules.Performance.dll)
Assembly Versions: 2.8.0.0