Edit

Share via


CA1806: Do not ignore method results

Property Value
Rule ID CA1806
Title Do not ignore method results
Category Performance
Fix is breaking or non-breaking Non-breaking
Enabled by default in .NET 9 As suggestion

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's never used.

  • A language-integrated query (LINQ) method that returns a result that's 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 return a new instance of a string instead of modifying the instance of the string in the calling method.

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

LINQ methods are known to not have side effects, and the result should not be ignored.

How to fix violations

If a method creates a new instance of an object that's 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 it.

-or-

If method A calls method B but does not use the new string instance that method B returns, pass the instance as an argument to another method or assign the instance to a variable. Or remove the call if it's 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.

-or-

If a LINQ method A calls method B but does not use the result, 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.

Suppress a warning

If you just want to suppress a single violation, add preprocessor directives to your source file to disable and then re-enable the rule.

#pragma warning disable CA1806
// The code that's violating the rule is on this line.
#pragma warning restore CA1806

To disable the rule for a file, folder, or project, set its severity to none in the configuration file.

[*.{cs,vb}]
dotnet_diagnostic.CA1806.severity = none

For more information, see How to suppress code analysis warnings.

Configure code to analyze

Use the following option to configure which parts of your codebase to run this rule on.

Additional methods to enforce

You can configure this rule to check that results from additional custom APIs are used. Specify one or more methods as the value of the additional_use_results_methods option. To specify multiple method names, separate them with |. The allowable formats for the method name are:

  • Method name only (which will include all methods with that name, regardless of their containing type or namespace).
  • Fully qualified name in the documentation ID format, with an optional M: prefix.

For example, to specify that rule CA1806 should also check that the result from a method named MyMethod1 is used, add the following key-value pair to an .editorconfig file in your project.

dotnet_code_quality.CA1806.additional_use_results_methods = MyMethod1

Or, use the fully qualified name to disambiguate or ensure that only a specific method with that name is included.

dotnet_code_quality.CA1806.additional_use_results_methods = M:MyNamespace.MyType.MyMethod1(ParamType)

Example 1

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

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; }
    }
}
Public Class Book
    Public Sub New(ByVal title As String)

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

        Me.Title = title

    End Sub

    Public ReadOnly Property Title() As String

End Class

Example 2

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

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; }
    }
}
Public Class Book
    Public Sub New(ByVal title As String)

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

        Me.Title = title

    End Sub

    Public ReadOnly Property Title() As String

End Class

Example 3

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

Note

This violation cannot be reproduced in Visual Basic.

public class Book
{
    public Book()
    {
    }

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

Example 4

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

public class Book
{
    public Book()
    {
    }

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