不要忽略方法结果
更新:2007 年 11 月
TypeName |
DoNotIgnoreMethodResults |
CheckId |
CA1806 |
类别 |
Microsoft.Usage |
是否重大更改 |
否 |
原因
创建了新对象,但从未使用它。
- 或 -
调用了创建并返回新字符串的方法,但从未使用此新字符串。
- 或 -
COM 或 P/Invoke 方法返回的 HRESULT 或错误代码从未使用过。
规则说明
此规则帮助您注意某些情形,例如代码中出现的以下情形:
创建不必要的对象以及与未使用的对象相关的垃圾回收会导致性能下降。
字符串是不可变的。诸如 ToUpper 之类的方法返回字符串的新实例,而不是在调用方法中修改字符串实例。
如果忽略 HRESULT 或错误代码,可能会导致在出错或资源不足的情况下出现意外行为。
如何修复冲突
如果方法 A 创建了 B 对象的一个新实例,但该实例从未使用过,请将该实例作为参数传递给其他方法或将该实例赋给一个变量。如果没有必要创建此对象,请移除它。
- 或 -
方法 A 调用了方法 B,但并未使用方法 B 返回的新字符串实例。请将此实例作为参数传递给其他方法并将此实例赋给一个变量,或者,如果没有必要进行此调用,请移除此调用。
- 或 -
方法 A 调用了方法 B,但并未使用该方法返回的 HRESULT 或错误代码。请在条件语句中使用此结果,将此结果赋给一个变量,或将其作为参数传递给其他方法。
何时禁止显示警告
不要禁止显示此规则发出的警告,除非创建对象的行为有特定的用途。
示例
下面的示例演示一个忽略调用 Trim 所得结果的类。
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; }
}
};
}
下面的示例通过将对一个变量调用 Trim 所得到的结果赋回此变量,修复了前面的冲突。
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; }
}
};
}
下面的示例显示了一个方法,此方法不使用其创建的一个对象。
说明: |
---|
此冲突在 Visual Basic 中无法重现。 |
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();
}
};
}
下面的示例通过移除不必要的对象创建操作修复了前面的冲突。
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();
}
};
}
下面的示例演示一个方法,此方法忽略了本机方法 GetShortPathName 返回的错误代码。
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();
}
};
}
下面的示例通过在调用失败时检查错误代码并引发异常修复了前面的冲突。
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();
}
};
}