Gendarme.Rules.Performance.AvoidUnneededFieldInitializationRule Class
This rule looks for constructors that assign fields to their default value (e.g. 0 for an integer, null for an object or a string). Since the CLR zero initializes all values there is no need, under most circumstances, to assign default values. Doing so only adds size to source code and in IL.

See Also: AvoidUnneededFieldInitializationRule 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", "CA1805:DoNotInitializeUnnecessarily")]
[Gendarme.Framework.Problem("This constructor needlessly initializes zero initializes some fields.")]
[Gendarme.Framework.Solution("Remove the unneeded initialization from the constructors.")]
public class AvoidUnneededFieldInitializationRule : Gendarme.Framework.Rule, Gendarme.Framework.IMethodRule

Remarks

This rule is available since Gendarme 2.2

Example

Bad example:

Example

            public class Bad {
            	int i;
            	string s;
            	public Bad ()
            	{
            		i = 0;
            		s = null;
            	}
            }
            

Example

Good example:

Example

            public class Good {
            	int i;
            	string s;
            	public Good ()
            	{
            		// don't assign 'i' since it's already 0
            		// but we might prefer to assign a string to String.Empty
            		s = String.Empty;
            	}
            }
            

Requirements

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