Click to Rate and Give Feedback
MSDN
MSDN Library
Visual Studio 2005
Usage Warnings
 Do not ignore method results
This page is specific to
Microsoft Visual Studio 2005/.NET Framework 2.0

Other versions are also available for the following:
Visual Studio Team System
Do not ignore method results

TypeName

DoNotIgnoreMethodResults

CheckId

CA1806

Category

Microsoft.Performance

Breaking Change

NonBreaking

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.

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

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

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

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 What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Currently only fires on String methods      David M. Kean - MSFT   |   Edit   |  

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 What's this?: Add a tag
Flag as ContentBug
Example - Firing on a call to one of the string methods      David M. Kean - MSFT   |   Edit   |  

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 What's this?: Add a tag
Flag as ContentBug
Example - Firing on the creation of a new object      David M. Kean - MSFT   |   Edit   |  

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 What's this?: Add a tag
Flag as ContentBug
Processing
© 2008 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker