Do not cast unnecessarily







Breaking Change

Non Breaking

A method performs duplicate casts on one of its arguments or local variables. For complete analysis by this rule, the tested assembly must be built with debugging information and the associated program database (.pdb) file must be available.

Duplicate casts degrade performance, especially when the casts are performed in compact iteration statements. For explicit duplicate cast operations, store the result of the cast in a local variable and use the local variable instead of the duplicate cast operations.

If the C# is operator is used to test whether the cast will succeed before the actual cast is performed, consider testing the result of the as operator instead. This provides the same functionality without the implicit cast operation performed by the is operator.

To fix a violation of this rule, modify the method implementation to minimize the number of cast operations.

It is safe to suppress a warning from this rule, or ignore the rule entirely, if performance is not a concern.

The following example shows a method that violates the rule using the C# is operator. A second method satisfies the rule by replacing the is operator with a test against the result of the as operator, which decreases the number of cast operations per iteration from two to one.

using System;
using System.Collections;
using System.Windows.Forms;

namespace PerformanceLibrary
   public sealed class SomeClass
      private SomeClass() {}

      // This method violates the rule. 
      public static void UnderPerforming(ArrayList list)
         foreach(object obj in list) 
            // The 'is' statement performs a cast operation. 
            if(obj is Control) 
               // The 'as' statement performs a duplicate cast operation.
               Control aControl = obj as Control;
               // Use aControl.


      // This method satisfies the rule. 
      public static void BetterPerforming(ArrayList list)
         foreach(object obj in list) 
            Control aControl = obj as Control;
            if(aControl != null) 
               // Use aControl.

The following example shows a method, start_Click, with multiple duplicate explicit casts, which violates the rule, and a method, reset_Click, which satisfies the rule by storing the cast in a local variable.

Imports System
Imports System.Drawing
Imports System.Windows.Forms

Namespace PerformanceLibrary

   Public Class SomeForm : Inherits Form

      Dim start, reset As Button

      Sub New()

         start = New Button()
         reset = New Button()
         AddHandler start.Click, AddressOf start_Click
         AddHandler reset.Click, AddressOf reset_Click

      End Sub 

      ' This method violates the rule. 
      Private Sub start_Click(sender As Object, e As EventArgs)

         Dim controlSize As Size = DirectCast(sender, Control).Size
         Dim rightToLeftValue As RightToLeft = _ 
            DirectCast(sender, Control).RightToLeft
         Dim parent As Control = DirectCast(sender, Control)

      End Sub 

      ' This method satisfies the rule. 
      Private Sub reset_Click(sender As Object, e As EventArgs)

         Dim someControl As Control = DirectCast(sender, Control)
         Dim controlSize As Size = someControl.Size
         Dim rightToLeftValue As RightToLeft = someControl.RightToLeft
         Dim parent As Control = someControl

      End Sub 

   End Class 

End Namespace

Community Additions