Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All
Expand Minimize

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.

Imports System
Imports System.Globalization

Namespace PerformanceLibrary

   Public Class IgnoredMethodResults
   
      Sub Violations()
      
         Dim violationOne As String = "lo"
         Dim violationTwo As String = "MediuM "

         violationOne.ToUpper(CultureInfo.InvariantCulture)
         violationTwo.ToLower(CultureInfo.InvariantCulture).Trim()

      End Sub

   End Class

End Namespace

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:
© 2015 Microsoft