CA1819:属性不应返回数组
类型名 |
PropertiesShouldNotReturnArrays |
CheckId |
CA1819 |
类别 |
Microsoft.Performance |
是否重大更改 |
是 |
原因
公共类型中的公共或受保护属性返回一个数组。
规则说明
即使属性是只读的,该属性返回的数组也不是写保护的。 若要使数组不会被更改,属性必须返回数组的副本。 通常,用户不能理解调用这种属性的负面性能影响。 特别是,他们可能将该属性用作索引属性。
如何解决冲突
若要修复与该规则的冲突,请将属性更改为方法,或将属性更改为返回一个集合。
何时禁止显示警告
特性可以包含返回数组的属性,但不能包含返回集合的属性。 可以禁止显示从 [System.Attribute] 类派生的特性的属性所引发的警告。 否则,不要禁止显示此规则发出的警告。
冲突的示例
说明
下面的示例演示一个与该规则冲突的属性。
代码
Imports System
Namespace PerformanceLibrary
Public Class Book
Private _Pages As String()
Public Sub New(ByVal pages As String())
_Pages = pages
End Sub
Public ReadOnly Property Pages() As String()
Get
Return _Pages
End Get
End Property
End Class
End Namespace
using System;
namespace PerformanceLibrary
{
public class Book
{
private string[] _Pages;
public Book(string[] pages)
{
_Pages = pages;
}
public string[] Pages
{
get { return _Pages; }
}
}
}
注释
若要修复与该规则的冲突,请将属性更改为方法,或将属性更改为返回一个集合而不是数组。
将属性更改为方法的示例
说明
下面的示例通过将属性更改为方法来修复冲突。
代码
Imports System
Namespace PerformanceLibrary
Public Class Book
Private _Pages As String()
Public Sub New(ByVal pages As String())
_Pages = pages
End Sub
Public Function GetPages() As String()
' Need to return a clone of the array so that consumers
' of this library cannot change its contents
Return DirectCast(_Pages.Clone(), String())
End Function
End Class
End Namespace
using System;
namespace PerformanceLibrary
{
public class Book
{
private string[] _Pages;
public Book(string[] pages)
{
_Pages = pages;
}
public string[] GetPages()
{
// Need to return a clone of the array so that consumers
// of this library cannot change its contents
return (string[])_Pages.Clone();
}
}
}
返回集合的示例。
说明
下面的示例通过以下方法修复冲突:将属性更改为返回一个
ReadOnlyCollection.
代码
Imports System
Imports System.Collections.ObjectModel
Namespace PerformanceLibrary
Public Class Book
Private _Pages As ReadOnlyCollection(Of String)
Public Sub New(ByVal pages As String())
_Pages = New ReadOnlyCollection(Of String)(pages)
End Sub
Public ReadOnly Property Pages() As ReadOnlyCollection(Of String)
Get
Return _Pages
End Get
End Property
End Class
End Namespace
using System;
using System.Collections.ObjectModel;
namespace PerformanceLibrary
{
public class Book
{
private ReadOnlyCollection<string> _Pages;
public Book(string[] pages)
{
_Pages = new ReadOnlyCollection<string>(pages);
}
public ReadOnlyCollection<string> Pages
{
get { return _Pages; }
}
}
}
允许用户修改属性
说明
您可能希望允许类的使用者修改属性。 下面的示例演示一个与该规则冲突的读/写属性。
代码
Imports System
Namespace PerformanceLibrary
Public Class Book
Private _Pages As String()
Public Sub New(ByVal pages As String())
_Pages = pages
End Sub
Public Property Pages() As String()
Get
Return _Pages
End Get
Set(ByVal value as String())
_Pages = value
End Set
End Property
End Class
End Namespace
using System;
namespace PerformanceLibrary
{
public class Book
{
private string[] _Pages;
public Book(string[] pages)
{
_Pages = pages;
}
public string[] Pages
{
get { return _Pages; }
set { _Pages = value; }
}
}
}
注释
下面的示例通过将属性更改为返回一个 Collection 来修复冲突。
代码
Imports System
Imports System.Collections.ObjectModel
Namespace PerformanceLibrary
Public Class Book
Private _Pages As Collection(Of String)
Public Sub New(ByVal pages As String())
_Pages = New Collection(Of String)(pages)
End Sub
Public ReadOnly Property Pages() As Collection(Of String)
Get
Return _Pages
End Get
End Property
End Class
End Namespace
using System;
using System.Collections.ObjectModel;
namespace PerformanceLibrary
{
public class Book
{
private Collection<string> _Pages;
public Book(string[] pages)
{
_Pages = new Collection<string>(pages);
}
public Collection<string> Pages
{
get { return _Pages; }
}
}
}