title: VS2010 Code Analysis and CA1800 : Microsoft Performance
body: <p>I have the code below which I know isn't optimal. I ran the Code Analysis and it gave me the warning</p>
  
  <pre><code>CA1800 : Microsoft.Performance : 'customField', a variable, is cast to type 'DateCustomFieldRef' multiple times in method 'Customer.CustomerToUpdat(SearchResult)'. Cache the result of the 'as' operator or direct cast in order to eliminate the redundant castclass instruction. 
  </code></pre>
  
  <p>and I really don't understand what to do.</p>
  
  <pre><code>CustomFieldRef[] customFields = customer.customFieldList;
  for (int f = 3; f &lt; customFields.Length; f++)
  {
      CustomFieldRef customField = customFields[f];
      if (customField is DateCustomFieldRef)
          {
              DateCustomFieldRef dateField = (DateCustomFieldRef)customField;
                  if (dateField.internalId != null &amp;&amp; dateField.internalId == "created_date")
                      {
                              createdDate = dateField.value.ToString();
                          }
                  }
                  if (customField is StringCustomFieldRef)
                  {
                      StringCustomFieldRef tradingNameField = (StringCustomFieldRef)customField;
                          if (businessNameField.internalId != null &amp;&amp; businessNameField.internalId == "business_name")
                          {
                              businessName = businessNameField.value;
                      }
                  }
          }
  }
  </code></pre>
  
  <p>Could someone please give me a code example or explain further what it really means?</p>
  
  <p>Thanks in advance.</p>