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 zeroize
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
Gendarme.Framework.Rule
Gendarme.Rules.Performance.AvoidUnneededFieldInitializationRule
[Gendarme.Framework.EngineDependency(typeof(Gendarme.Framework.Engines.OpCodeEngine, Gendarme.Framework, Version=2.4.2.0, Culture=neutral, PublicKeyToken=null))]
[Gendarme.Framework.FxCopCompatibility("Microsoft.Performance", "CA1805:DoNotInitializeUnnecessarily")]
[Gendarme.Framework.Problem("This constructor needlessly initialize some fields to their default value.")]
[Gendarme.Framework.Solution("Remove the unneeded initialization from the constructors.")]
public class
AvoidUnneededFieldInitializationRule :
Gendarme.Framework.Rule,
Gendarme.Framework.IMethodRule
Bad example:
Example
public class Bad {
int i;
string s;
public Bad ()
{
i = 0;
s = null;
}
}
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;
}
}
Namespace: Gendarme.Rules.Performance
Assembly: Gendarme.Rules.Performance (in Gendarme.Rules.Performance.dll)
Assembly Versions: 2.4.2.0