Visual Studio Team System
Do not ignore method results

TypeName

DoNotIgnoreMethodResults

CheckId

CA1806

Category

Microsoft.Performance

Breaking Change

NonBreaking

Cause

A constructor or method is called that creates and returns a new object, and the new object is neither used nor assigned to a variable.

Rule Description

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

How to Fix Violations

To fix a violation of this rule, use the new object or remove the method or constructor call.

When to Exclude Warnings

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

Visual Basic
Imports System
Imports System.Globalization

Namespace PerformanceLibrary

   Public Class IgnoredMethodResults
   
      Sub Violations()
      
         Dim violationOne As String = "lo"
         Dim violationTwo As String = "MediuM "

         violationOne.ToUpper(CultureInfo.InvariantCulture)
         violationTwo.ToLower(CultureInfo.InvariantCulture).Trim()

      End Sub

   End Class

End Namespace
C#
using System;
using System.Globalization;

namespace PerformanceLibrary
{
   public class IgnoredMethodResults
   {
      public void Violations()
      {
         "violationOne".ToUpper(CultureInfo.InvariantCulture);

         string violationTwo = "MediuM ";
         violationTwo.ToLower(CultureInfo.InvariantCulture).Trim();

         new Random();
      }
   }
}

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

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

Tags :


Community Content

David M. Kean - MSFT
Currently only fires on String methods

This rule currently only fires on string methods that return a new instance of a string. This includes methods such as, but not limited to, String.Trim, String.ToLower, String.ToUpper, etc.

As strings are immutable (that is, they can't be changed), these methods return a new instance of string rather than modifying the orginal instance the method was called on.

The following example shows this.

[C#]
 
string upper = "UPPER";
string lower = upper.ToLower();
 
Console.WriteLine(upper);
Console.WriteLine(lower);

 

This outputs the following:

UPPER
upper
Tags :

David M. Kean - MSFT
Example - Firing on a call to one of the string methods

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

[C#]
 
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; }
        }
    }
}

 

[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

 

[C++]
 
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; }
        }
    };
}

 

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

[C#]
 
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; }
        }
    }
}

 

[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
                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
 

 

[C++]
 
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; }
        }
    };
}
 
Tags :

David M. Kean - MSFT
Example - Firing on the creation of a new object

The following example shows a method that does not use the creation of an object.

[C#]
 
using System;
 
namespace Samples
{
public class Book
{
public Book()
{
}
 
        public static Book CreateBook()
{
// Violates this rule
new Book();
return new Book();
}
}
}

[Visual Basic]
 
This violation cannot be reproduced in Visual Basic.

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

The following example fixes the above violation by removing the unnecessary creation.

[C#]
  
using System;
 
namespace Samples
{
public class Book
{
public Book()
{
}
 
        public static Book CreateBook()
{
return new Book();
}
}
}

[Visual Basic]
 
This violation cannot be reproduced in Visual Basic.

[C++]
 
using namespace System;
 
namespace Samples
{
public ref class Book
{
 
    public:
        Book()
{
}
 
        static Book^ CreateBook()
{
return gcnew Book();
}
};
}
Tags :

Page view tracker