Gendarme.Rules.Performance.AvoidUnneededUnboxingRule Class
This rule checks methods which unbox the same value type multiple times (i.e. the value is copied from the heap into the stack). Because the copy is relatively expensive, the code should be rewritten to minimize unboxes. For example, using a local variable of the right value type should remove the need for more than one unbox instruction per variable.

See Also: AvoidUnneededUnboxingRule Members

Syntax

[Gendarme.Framework.EngineDependency(typeof(Gendarme.Framework.Engines.OpCodeEngine, Gendarme.Framework, Version=2.8.0.0, Culture=neutral, PublicKeyToken=null))]
[Gendarme.Framework.Problem("This method unboxes (converts from object to a value type) the same value multiple times.")]
[Gendarme.Framework.Solution("Cast the variable, once, into a temporary variable and use the temporary.")]
public class AvoidUnneededUnboxingRule : Gendarme.Framework.Rule, Gendarme.Framework.IMethodRule

Remarks

This rule is available since Gendarme 2.0

Example

Bad example:

Example

            public struct Message {
            	private int msg;
            	private IntPtr hwnd, lParam, wParam, IntPtr result;
            	public override bool Equals (object o)
            	{
            		bool result = (this.msg == ((Message) o).msg);
            		result &= (this.hwnd == ((Message) o).hwnd);
            		result &= (this.lParam == ((Message) o).lParam);
            		result &= (this.wParam == ((Message) o).wParam);
            		result &= (this.result == ((Message) o).result);
            		return result;
            	}
            }
            

Example

Good example:

Example

            public struct Message {
            	private int msg;
            	private IntPtr hwnd, lParam, wParam, IntPtr result;
            	public override bool Equals (object o)
            	{
            		Message msg = (Message) o;
            		bool result = (this.msg == msg.msg);
            		result &= (this.hwnd == msg.hwnd);
            		result &= (this.lParam == msg.lParam);
            		result &= (this.wParam == msg.wParam);
            		result &= (this.result == msg.result);
            		return result;
            	}
            }
            

Requirements

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