C#idiomatic C#
ca1806fundamentals/code-analysis/quality-rules

Code analysis

CA1806: Do not ignore method results

Learn about code analysis rule CA1806: Do not ignore method results

CA1806 Performance enabled by default in .NET 10: As suggestion
Fix is breaking or non-breaking Non-breakingApplicable languages C# and Visual Basic

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 returns a HRESULT or error code that's never used.
  • A language-integrated query (LINQ) method returns a result that's never used.
  • A [Pure] method is called and the return value 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 System.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 and methods annotated with System.Diagnostics.Contracts.PureAttribute 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 method A calls a LINQ or pure 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.

csharp
#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.

ini
[*.{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.

ini
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.

ini
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 System.String.Trim.

sample · csharpdocs/fundamentals/code-analysis/quality-rules/snippets/csharp/all-rules/ca1806.cssnippet1

csharp
namespace ca1806
{
    //<snippet1>
    public class Book
    {
        public Book(string title)
        {
            // Violates this rule.
            title?.Trim();

            Title = title;
        }

        public string? Title { get; }
    }
    //</snippet1>
}

namespace ca1806_2
{
    //<snippet2>
    public class Book
    {
        public Book(string title)
        {
            // Fixes the violation.
            Title = title?.Trim();
        }

        public string? Title { get; }
    }
    //</snippet2>
}

namespace ca1806_3
{
    //<snippet3>
    public class Book
    {
        public Book() { }

        public static Book CreateBook()
        {
            // Violates this rule.
            new Book();
            return new Book();
        }
    }
    //</snippet3>
}

namespace ca1806_4
{
    //<snippet4>
    public class Book
    {
        public Book() { }

        public static Book CreateBook()
        {
            // Fixes the violation.
            return new Book();
        }
    }
    //</snippet4>
}

sample · vbdocs/fundamentals/code-analysis/quality-rules/snippets/vb/all-rules/ca1806-do-not-ignore-method-results_1.vbsnippet1

vb
Imports System

Namespace ca1806
    '<snippet1>
    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
    '</snippet1>

End Namespace

Namespace ca1806_2

    '<snippet2>
    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
    '</snippet2>

End Namespace

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

sample · csharpdocs/fundamentals/code-analysis/quality-rules/snippets/csharp/all-rules/ca1806.cssnippet2

csharp
namespace ca1806
{
    //<snippet1>
    public class Book
    {
        public Book(string title)
        {
            // Violates this rule.
            title?.Trim();

            Title = title;
        }

        public string? Title { get; }
    }
    //</snippet1>
}

namespace ca1806_2
{
    //<snippet2>
    public class Book
    {
        public Book(string title)
        {
            // Fixes the violation.
            Title = title?.Trim();
        }

        public string? Title { get; }
    }
    //</snippet2>
}

namespace ca1806_3
{
    //<snippet3>
    public class Book
    {
        public Book() { }

        public static Book CreateBook()
        {
            // Violates this rule.
            new Book();
            return new Book();
        }
    }
    //</snippet3>
}

namespace ca1806_4
{
    //<snippet4>
    public class Book
    {
        public Book() { }

        public static Book CreateBook()
        {
            // Fixes the violation.
            return new Book();
        }
    }
    //</snippet4>
}

sample · vbdocs/fundamentals/code-analysis/quality-rules/snippets/vb/all-rules/ca1806-do-not-ignore-method-results_1.vbsnippet2

vb
Imports System

Namespace ca1806
    '<snippet1>
    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
    '</snippet1>

End Namespace

Namespace ca1806_2

    '<snippet2>
    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
    '</snippet2>

End Namespace

Example 2

The following example shows a method that doesn't use an object that it creates.

sample · csharpdocs/fundamentals/code-analysis/quality-rules/snippets/csharp/all-rules/ca1806.cssnippet3

csharp
namespace ca1806
{
    //<snippet1>
    public class Book
    {
        public Book(string title)
        {
            // Violates this rule.
            title?.Trim();

            Title = title;
        }

        public string? Title { get; }
    }
    //</snippet1>
}

namespace ca1806_2
{
    //<snippet2>
    public class Book
    {
        public Book(string title)
        {
            // Fixes the violation.
            Title = title?.Trim();
        }

        public string? Title { get; }
    }
    //</snippet2>
}

namespace ca1806_3
{
    //<snippet3>
    public class Book
    {
        public Book() { }

        public static Book CreateBook()
        {
            // Violates this rule.
            new Book();
            return new Book();
        }
    }
    //</snippet3>
}

namespace ca1806_4
{
    //<snippet4>
    public class Book
    {
        public Book() { }

        public static Book CreateBook()
        {
            // Fixes the violation.
            return new Book();
        }
    }
    //</snippet4>
}

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

sample · csharpdocs/fundamentals/code-analysis/quality-rules/snippets/csharp/all-rules/ca1806.cssnippet4

csharp
namespace ca1806
{
    //<snippet1>
    public class Book
    {
        public Book(string title)
        {
            // Violates this rule.
            title?.Trim();

            Title = title;
        }

        public string? Title { get; }
    }
    //</snippet1>
}

namespace ca1806_2
{
    //<snippet2>
    public class Book
    {
        public Book(string title)
        {
            // Fixes the violation.
            Title = title?.Trim();
        }

        public string? Title { get; }
    }
    //</snippet2>
}

namespace ca1806_3
{
    //<snippet3>
    public class Book
    {
        public Book() { }

        public static Book CreateBook()
        {
            // Violates this rule.
            new Book();
            return new Book();
        }
    }
    //</snippet3>
}

namespace ca1806_4
{
    //<snippet4>
    public class Book
    {
        public Book() { }

        public static Book CreateBook()
        {
            // Fixes the violation.
            return new Book();
        }
    }
    //</snippet4>
}