Export (0) Print
Expand All
Expand Minimize
1 out of 2 rated this helpful - Rate this topic

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.

NoteNote

This violation cannot be reproduced in Visual Basic.


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; }
        }
    }
}


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


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; }
        }
    }
}


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


using System;

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

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


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


using System;

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

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


Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.