Gendarme.Rules.Performance.OverrideValueTypeDefaultsRule Class
This rule checks all value types, except enumerations, to see if they use the default implementation of Equals(object) and GetHashCode() methods. While ValueType implementations work for any value type they do so at the expense of performance (the default implementation uses reflection to access fields). You can easily override both methods with much faster code since you know all meaningful fields inside your structure. At the same time you should also provide, if your language allows it, operator overloads for equality (op_Equality, ==) and inequality (op_Inequality, !=).

See Also: OverrideValueTypeDefaultsRule Members

Syntax

[Gendarme.Framework.FxCopCompatibility("Microsoft.Performance", "CA1815:OverrideEqualsAndOperatorEqualsOnValueTypes")]
[Gendarme.Framework.Problem("This type does not override the default ValueType implementation of Equals(object) and GetHashCode() which are quite slow.")]
[Gendarme.Framework.Solution("To avoid performance penalities of the default implementations you should override, or implement, the specified methods.")]
public class OverrideValueTypeDefaultsRule : Gendarme.Framework.Rule, Gendarme.Framework.ITypeRule

Remarks

This rule is available since Gendarme 2.0

Example

Bad example:

Example

            public struct Coord {
            	int X, Y, Z;
            }
            

Example

Good example:

Example

            public struct Coord {
            	int X, Y, Z;
            	public override bool Equals (object obj)
            	{
            		if (obj == null) {
            			return false;
            		}
            		Coord c = (Coord)obj;
            		return ((X == c.X) && (Y == c.Y) && (Z == c.Z));
            	}
            	public override int GetHashCode ()
            	{
            		return X ^ Y ^ Z;
            	}
            	public static bool operator == (Coord left, Coord right)
            	{
            		return left.Equals (right);
            	}
            	public static bool operator != (Coord left, Coord right)
            	{
            		return !left.Equals (right);
            	}
            }
            

Requirements

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