Hinweis
Für den Zugriff auf diese Seite ist eine Autorisierung erforderlich. Sie können versuchen, sich anzumelden oder das Verzeichnis zu wechseln.
Für den Zugriff auf diese Seite ist eine Autorisierung erforderlich. Sie können versuchen, das Verzeichnis zu wechseln.
Aktualisiert: November 2007
TypeName |
DoNotIgnoreMethodResults |
CheckId |
CA1806 |
Kategorie |
Microsoft.Usage |
Unterbrechende Änderung |
Nicht unterbrechend |
Ursache
Es wurde ein neues Objekt erstellt, jedoch nie verwendet.
- oder -
Es wird eine Methode aufgerufen, die eine neue Zeichenfolge erstellt und zurückgibt, allerdings wird die neue Zeichenfolge nie verwendet.
- oder -
Eine COM- oder P/Invoke-Methode, die ein HRESULT oder einen Fehlercode zurückgibt, der nie verwendet wird.
Regelbeschreibung
Mit dieser Regel wird u. a. auf folgende Situationen aufmerksam gemacht, die im Code auftreten können:
Die unnötige Objekterstellung und die zugeordnete Garbage Collection des nicht verwendeten Objekts beeinträchtigen die Leistung.
Zeichenfolgen sind unveränderlich. Methoden wie ToUpper geben eine neue Instanz einer Zeichenfolge zurück, anstatt die Instanz der Zeichenfolge in der aufrufenden Methode zu ändern.
Wenn HRESULT oder der Fehlercode ignoriert wird, kann dies zu unerwartetem Verhalten in Fehlerbedingungen oder Situationen führen, in denen nur geringe Ressourcen verfügbar sind.
Behandlung von Verstößen
Wenn Methode A eine neue Instanz von Objekt B erstellt, das nie verwendet wird, übergeben Sie die Instanz als Argument an eine andere Methode, oder weisen Sie die Instanz einer Variablen zu. Wenn die Objekterstellung unnötig ist, entfernen Sie sie.
- oder -
Methode A ruft Methode B auf, verwendet jedoch nicht die neue Zeichenfolgeninstanz, die von Methode B zurückgegeben wird. Übergeben Sie die Instanz als Argument an eine andere Methode, weisen Sie die Instanz einer Variablen zu, oder entfernen Sie den Aufruf, wenn dieser nicht erforderlich ist.
- oder -
Methode A ruft Methode B auf, verwendet jedoch nicht HRESULT oder den Fehlercode, der von der Methode zurückgegeben wird. Verwenden Sie das Ergebnis in einer Bedingungsanweisung, weisen Sie das Ergebnis einer Variablen zu, oder übergeben Sie es als ein Argument an eine andere Methode.
Wann sollten Warnungen unterdrückt werden?
Unterdrücken Sie keine Warnung dieser Regel, es sei denn, mit der Objekterstellung wird ein bestimmter Zweck verfolgt.
Beispiel
Das folgende Beispiel zeigt eine Klasse, die das Ergebnis des Aufrufs von Trim ignoriert.
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
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; }
}
}
}
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; }
}
};
}
Im folgenden Beispiel wird der vorherige Verstoß korrigiert, indem das Ergebnis von Trim wieder der Variablen, für die die Methode aufgerufen wurde, zugeordnet wird.
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
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; }
}
}
}
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; }
}
};
}
Im folgenden Beispiel wird eine Methode veranschaulicht, die kein von ihr erstelltes Objekt verwendet.
Hinweis: |
|---|
Dieser Verstoß kann nicht in Visual Basic reproduziert werden. |
using System;
namespace Samples
{
public class Book
{
public Book()
{
}
public static Book CreateBook()
{
// Violates this rule
new Book();
return new Book();
}
}
}
using namespace System;
namespace Samples
{
public ref class Book
{
public:
Book()
{
}
static Book^ CreateBook()
{
// Violates this rule
gcnew Book();
return gcnew Book();
}
};
}
Im folgenden Beispiel wird der vorherige Verstoß korrigiert, indem die unnötige Erstellung eines Objekts entfernt wird.
using System;
namespace Samples
{
public class Book
{
public Book()
{
}
public static Book CreateBook()
{
return new Book();
}
}
}
using namespace System;
namespace Samples
{
public ref class Book
{
public:
Book()
{
}
static Book^ CreateBook()
{
return gcnew Book();
}
};
}
Im folgenden Beispiel wird eine Methode dargestellt, die den Fehlercode ignoriert, der von der systemeigenen Methode GetShortPathName zurückgegeben wird.
Imports System
Imports System.ComponentModel
Imports System.IO
Imports System.Runtime.InteropServices
Imports System.Text
Namespace Samples
Public Module FileIO
Public Function GetShortPath(ByVal longPath As String) As String
longPath = Path.GetFullPath(longPath)
Const MAX_PATH As Integer = 260
Dim shortPathBuffer As New StringBuilder(MAX_PATH)
' Violates this rule
NativeMethods.GetShortPathName(longPath, shortPathBuffer, shortPathBuffer.Capacity)
Return shortPathBuffer.ToString()
End Function
End Module
Friend Module NativeMethods
<DllImport("kernel32.dll", CharSet:=CharSet.Auto, SetLastError:=True, BestFitMapping:=False, ThrowOnUnmappableChar:=True)> _
Public Function GetShortPathName(ByVal lpszLongPath As String, ByVal lpszShortPath As StringBuilder, ByVal cchBuffer As UInteger) As UInteger
End Function
End Module
End Namespace
using System;
using System.ComponentModel;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
namespace Samples
{
public static class FileIO
{
public static string GetShortPath(string longPath)
{
longPath = Path.GetFullPath(longPath);
const int MAX_PATH = 260;
StringBuilder shortPathBuffer = new StringBuilder(MAX_PATH);
// Violates this rule
NativeMethods.GetShortPathName(longPath, shortPathBuffer, (uint)shortPathBuffer.Capacity);
return shortPathBuffer.ToString();
}
}
internal static class NativeMethods
{
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true, BestFitMapping = false, ThrowOnUnmappableChar = true)]
public static extern uint GetShortPathName(string lpszLongPath, StringBuilder lpszShortPath, uint cchBuffer);
}
}
#include "stdafx.h"
using namespace System;
using namespace System::ComponentModel;
using namespace System::IO;
using namespace System::Runtime::InteropServices;
using namespace System::Text;
namespace Samples
{
private ref class NativeMethods sealed
{
private:
NativeMethods()
{
}
internal:
[DllImport("kernel32.dll", CharSet=CharSet::Auto, SetLastError=true, BestFitMapping=false, ThrowOnUnmappableChar=true)]
static unsigned int GetShortPathName(String^ lpszLongPath, StringBuilder^ lpszShortPath, unsigned int cchBuffer);
};
public ref class FileIO sealed
{
private:
FileIO()
{
}
public:
static String^ GetShortPath(String^ longPath)
{
longPath = Path::GetFullPath(longPath);
const int MAX_PATH = 260;
StringBuilder^ shortPathBuffer = gcnew StringBuilder(MAX_PATH);
// Violates this rule
NativeMethods::GetShortPathName(longPath, shortPathBuffer, shortPathBuffer->Capacity);
return shortPathBuffer->ToString();
}
};
}
Im folgenden Beispiel wird der vorherige Verstoß korrigiert, indem der Fehlercode überprüft wird und eine Ausnahme ausgelöst wird, wenn der Aufruf fehlschlägt.
Namespace Samples
Public Module FileIO_1
Public Function GetShortPath(ByVal longPath As String) As String
longPath = Path.GetFullPath(longPath)
Const MAX_PATH As Integer = 260
Dim shortPathBuffer As New StringBuilder(MAX_PATH)
' GetShortPathName returns 0 when the operation fails
If NativeMethods.GetShortPathName(longPath, shortPathBuffer, shortPathBuffer.Capacity) = 0 Then
' Note: The constructor of Win32Exception will automatically
' set Win32Exception.NativeErrorCode to Marshal.GetLastWin32Error()
Throw New Win32Exception()
End If
Return shortPathBuffer.ToString()
End Function
End Module
Friend Module NativeMethods_1
<DllImport("kernel32.dll", CharSet:=CharSet.Auto, SetLastError:=True, BestFitMapping:=False, ThrowOnUnmappableChar:=True)> _
Public Function GetShortPathName(ByVal lpszLongPath As String, ByVal lpszShortPath As StringBuilder, ByVal cchBuffer As UInteger) As UInteger
End Function
End Module
End Namespace
namespace Samples
{
public static class FileIO_1
{
public static string GetShortPath(string longPath)
{
const int MAX_PATH = 260;
StringBuilder shortPathBuffer = new StringBuilder(MAX_PATH);
// GetShortPathName returns 0 when the operation fails
if (NativeMethods.GetShortPathName(longPath, shortPathBuffer, (uint)shortPathBuffer.Capacity) == 0)
{
// Note: The constructor of Win32Exception will automatically
// set Win32Exception.NativeErrorCode to Marshal.GetLastWin32Error()
throw new Win32Exception();
}
return shortPathBuffer.ToString();
}
}
internal static class NativeMethods_1
{
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true, BestFitMapping = false, ThrowOnUnmappableChar = true)]
public static extern uint GetShortPathName(string lpszLongPath, StringBuilder lpszShortPath, uint cchBuffer);
}
}
#include "stdafx.h"
using namespace System;
using namespace System::ComponentModel;
using namespace System::IO;
using namespace System::Runtime::InteropServices;
using namespace System::Text;
namespace Samples
{
private ref class NativeMethods_1 sealed
{
private:
void NativeMethods()
{
}
internal:
[DllImport("kernel32.dll", CharSet=CharSet::Auto, SetLastError=true, BestFitMapping=false, ThrowOnUnmappableChar=true)]
static unsigned int GetShortPathName(String^ lpszLongPath, StringBuilder^ lpszShortPath, unsigned int cchBuffer);
};
public ref class FileIO_1 sealed
{
private:
void FileIO()
{
}
public:
static String^ GetShortPath(String^ longPath)
{
longPath = Path::GetFullPath(longPath);
const int MAX_PATH = 260;
StringBuilder^ shortPathBuffer = gcnew StringBuilder(MAX_PATH);
// GetShortPathName returns 0 when the operation fails
if (NativeMethods::GetShortPathName(longPath, shortPathBuffer, shortPathBuffer->Capacity) == 0)
{
// Note: The constructor of Win32Exception will automatically
// set Win32Exception.NativeErrorCode to Marshal.GetLastWin32Error()
throw gcnew Win32Exception();
}
return shortPathBuffer->ToString();
}
};
}
Hinweis: