共用方式為


CA1800:請勿執行不必要的轉型

型別名稱

DoNotCastUnnecessarily

CheckId

CA1800

分類

Microsoft.Performance

中斷變更

中斷

原因

方法對其中一個引數或區域變數執行重複轉型。 若要依此規則完整分析,則必須使用偵錯資訊建置測試的組件 (Assembly),而且必須可以使用關聯的程式資料庫 (.pdb) 檔案。

規則描述

重複轉型會降低效能,尤其是在精簡型態的反覆運算陳述式中執行轉型時。 若為明確重複轉型作業,請將轉型結果儲存在區域變數中,並改用區域變數而不用重複轉型作業。

如果 C# is 運算子在執行實際轉型之前,用於測試轉型是否會成功,請考慮改為測試 as 運算子的結果。 這可提供相同的功能,而不需由 is 運算子執行隱含轉型作業。

如何修正違規

若要修正此規則的違規情形,請修改方法實作 (Implementation) 以最小化轉型作業的數目。

隱藏警告的時機

如果效能並非考量重點,則您可以放心地隱藏這項規則的警告,或是完全忽略該規則。

範例

下列範例會顯示使用 C# is 運算子的方法違反規則。 第二個方法會滿足規則,做法是以 as 運算子的結果測試取代 is 運算子,而將每個反覆運算的轉型作業數目由兩個減少到一個。

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.
            }
         }
      }
   }
}

下列範例會顯示違反規則的方法 start_Click (該方法具有多個重複明確轉型),以及滿足規則的方法 reset_Click (該方法會將轉型儲存在區域變數中)。

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
         Controls.Add(start)
         Controls.Add(reset)

      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
using System;
using System.Drawing;
using System.Windows.Forms;

namespace PerformanceLibrary
{
   public class SomeForm : Form
   {
      Button start, reset;

      public SomeForm()
      {
         start = new Button();
         reset = new Button();
         start.Click += new EventHandler(start_Click);
         reset.Click += new EventHandler(reset_Click);
         Controls.Add(start);
         Controls.Add(reset);
      }

      // This method violates the rule.
      void start_Click(object sender, EventArgs e)
      {
         Size controlSize = ((Control)sender).Size;
         RightToLeft rightToLeftValue = ((Control)sender).RightToLeft;
         Control parent = (Control)sender;
      }

      // This method satisfies the rule.
      void reset_Click(object sender, EventArgs e)
      {
         Control someControl = (Control)sender;
         Size controlSize = someControl.Size;
         RightToLeft rightToLeftValue = someControl.RightToLeft;
         Control parent = someControl;
      }
   }
}

請參閱

參考

as (C# 參考)

is (C# 參考)