CA1806: Do not ignore method results

Note

This article applies to Visual Studio 2015. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here

Item Value
TypeName DoNotIgnoreMethodResults
CheckId CA1806
Category Microsoft.Usage
Breaking Change Non Breaking

Cause

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.

How to Fix Violations

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.

When to Suppress Warnings

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

Example

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

Example

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

Example

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

Note

This violation cannot be reproduced in Visual Basic.

using namespace System;

namespace Samples
{
    public ref class Book   
    { 
        private:        
            initonly String^ _Title;

        public:
        Book(String^ title)        
        {               
            if (title != nullptr)            
            {                  
                // Violates this rule                
                title->Trim();            
            }
                _Title = title;        
            }

        property String^ Title        
        {            
            String^ get() { return _Title; }        
        }    
    };
}
using System;

namespace Samples
{
    public class Book
    {
        private readonly string _Title;

        public Book(string title)
        {
            if (title != null)
            {
                // Violates this rule                
                title.Trim();
            }

            _Title = title;
        }

        public string Title
        {
            get { return _Title; }
        }
    }
}
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

Example

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

using namespace System;

namespace Samples
{    
    public ref class Book    
    {    
    private:        
        initonly String^ _Title;
 
    public:
        Book(String^ title)        
        {               
            if (title != nullptr)            
            {                        
                title = title->Trim();            
            }
 
            _Title = title;        
        }
 
        property String^ Title        
        {            
            String^ get() { return _Title; }        
        }    
    };
}
using System;

namespace Samples
{
    public class Book
    {
        private readonly string _Title;

        public Book(string title)
        {
            if (title != null)
            {
                title = title.Trim();
            }

            _Title = title;
        }

        public string Title
        {
            get { return _Title; }
        }
    }
}
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

Example

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

using namespace System;

namespace Samples
{    
    public ref class Book    
    {
    public:
        Book()        
        {        
        }
 
        static Book^ CreateBook()        
        {
            // Violates this rule            
            gcnew Book();            
            return gcnew Book();        
        }    
    };
}
using System;

namespace Samples
{
    public class Book
    {
        public Book()
        {
        }

        public static Book CreateBook()
        {
            // Violates this rule            
            new Book();
            return new Book();
        }
    }
}

Example

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

using namespace System;
 
namespace Samples
{
    public ref class Book    
    {
    public:
        Book()        
        {           
        }
        static Book^ CreateBook()        
        {            
            return gcnew Book();        
        }            
    };
}
using System;

namespace Samples
{
    public class Book
    {
        public Book()
        {
        }

        public static Book CreateBook()
        {
            return new Book();
        }
    }
}