Do not ignore method results
TOC
Collapse the table of content
Expand the table of content

Do not ignore method results

TypeName

DoNotIgnoreMethodResults

CheckId

CA1806

Category

Microsoft.Performance

Breaking Change

NonBreaking

A constructor or method is called that creates and returns a new object, and the new object is neither used nor assigned to a variable.

Unnecessary object creation and the associated garbage collection of the unused object degrade performance.

To fix a violation of this rule, use the new object or remove the method or constructor call.

Do not exclude a warning from this rule unless the act of creating the object serves some purpose.

The following example shows a class that ignores the result of calling String.Trim.

using System;
using System.Globalization;

namespace PerformanceLibrary
{
   public class IgnoredMethodResults
   {
      public void Violations()
      {
         "violationOne".ToUpper(CultureInfo.InvariantCulture);

         string violationTwo = "MediuM ";
         violationTwo.ToLower(CultureInfo.InvariantCulture).Trim();

         new Random();
      }
   }
}

The following example fixes the above violation by assigning the result of String.Trim back to the variable it was called on.

The following example shows a method that does not use an object that it creates.

NoteNote

This violation cannot be reproduced in Visual Basic.

The following example fixes the above violation by removing the unnecessary creation of an object.

Community Additions

ADD
Show:
© 2016 Microsoft