CA1819:屬性不應傳回陣列
型別名稱 |
PropertiesShouldNotReturnArrays |
CheckId |
CA1819 |
分類 |
Microsoft.Performance |
中斷變更 |
中斷 |
原因
公用型別中之公用或保護的屬性會傳回陣列。
規則描述
即使屬性是唯讀,所傳回的陣列不會是寫入保護。若要保持陣列為防止遭他人修改,屬性必須傳回陣列複本。一般而言,使用者不了解呼叫這類屬性所造成的不良效能影響。使用者更有可能使用屬性做為索引屬性。
如何修正違規
若要修正這個規則的違規情形,請讓屬性成為方法,或是變更屬性以傳回集合。
隱藏警告的時機
屬性 (Attribute) 可以包含傳回陣列的屬性 (Property),但不能包含傳回集合的屬性 (Property)。您可以隱藏針對衍生自 [System.Attribute] 類別之屬性 (Attribute) 的屬性 (Property) 所引發的警告。否則,不要隱藏此規則的警告。
範例違規
描述
下列範例顯示違反此規則的屬性。
程式碼
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; }
}
}
}