Export (0) Print
Expand All
Expand Minimize

CA1806: Do not ignore method results

TypeName

DoNotIgnoreMethodResults

CheckId

CA1806

Category

Microsoft.Usage

Breaking Change

Non Breaking

There are several possible reasons for this warning:

  • A new object is created but never used.

  • A method that creates and returns a new string is called and the new string is never used.

  • A COM or P/Invoke method that returns a HRESULT or error code that is never used. Rule Description

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

Strings are immutable and methods such as String.ToUpper returns a new instance of a string instead of modifying the instance of the string in the calling method.

Ignoring HRESULT or error code can lead to unexpected behavior in error conditions or to low-resource conditions.

If method A creates a new instance of B object that is never used, pass the instance as an argument to another method or assign the instance to a variable. If the object creation is unnecessary, remove the it.-or-

If method A calls method B, but does not use the new string instance that the method B returns. Pass the instance as an argument to another method, assign the instance to a variable. Or remove the call if it is unnecessary.

-or-

If method A calls method B, but does not use the HRESULT or error code that the method returns. Use the result in a conditional statement, assign the result to a variable, or pass it as an argument to another method.

Do not suppress 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.

The following example fixes the previous 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.

Note Note

This violation cannot be reproduced in Visual Basic.

Imports System

Namespace Samples

    Public Class Book

        Private ReadOnly _Title As String 

        Public Sub New(ByVal title As String)

            If title IsNot Nothing Then 
                ' Violates this rule                
                title.Trim()
            End If

            _Title = title

        End Sub 

        Public ReadOnly Property Title() As String 
            Get 
                Return _Title
            End Get 
        End Property 

    End Class 

End Namespace

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

Imports System

Namespace Samples

    Public Class Book

        Private ReadOnly _Title As String 

        Public Sub New(ByVal title As String)

            If title IsNot Nothing Then
                title = title.Trim()
            End If

            _Title = title

        End Sub 

        Public ReadOnly Property Title() As String 
            Get 
                Return _Title
            End Get 
        End Property 

    End Class 

End Namespace

The following example shows a method that ignores the error code that the native method GetShortPathName returns.

No code example is currently available or this language may not be supported.

The following example fixes the previous violation by checking the error code and throwing an exception when the call fails.

No code example is currently available or this language may not be supported.
Show:
© 2014 Microsoft