Condividi tramite


CA1819: Le proprietà non devono restituire matrici

TypeName

PropertiesShouldNotReturnArrays

CheckId

CA1819

Category

Microsoft.Performance

Breaking Change

Breaking

Causa

Una proprietà pubblica o protetta in un tipo pubblico restituisce una matrice.

Descrizione della regola

Le matrici restituite dalle proprietà non sono protette in scrittura, anche se la proprietà è in sola lettura. Affinché la matrice sia protetta da eventuali alterazioni, la proprietà deve restituire una copia della matrice. In genere, gli utenti non comprendono le implicazioni negative sulle prestazioni derivanti dalla chiamata di tale proprietà. In particolare, potrebbero utilizzare la proprietà come una proprietà indicizzata.

Come correggere le violazioni

Per correggere una violazione di questa regola, trasformare la proprietà in metodo oppure modificare la proprietà affinché restituisca un insieme.

Esclusione di avvisi

Gli attributi possono contenere proprietà che restituiscono matrici, ma non proprietà che restituiscono insiemi. È possibile eliminare un avviso generato per una proprietà di un attributo derivante dalla classe [System.Attribute]. Altrimenti, non escludere un avviso da questa regola.

Esempio di violazione

Oggetto di descrizione

Nell'esempio riportato di seguito viene illustrata una proprietà che viola questa regola.

Codice

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; }        
        }    
    }
}

Commenti

Per correggere una violazione di questa regola, trasformare la proprietà in metodo oppure modificare la proprietà affinché restituisca un insieme anziché una matrice.

Esempio di trasformazione di una proprietà in metodo

Oggetto di descrizione

Nell'esempio seguente viene corretta la violazione cambiando la proprietà in metodo.

Codice

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();        
        }    
    }
}

Esempio di restituzione di un insieme

Oggetto di descrizione

Nell'esempio seguente viene corretta la violazione cambiando la proprietà in modo che restituisca un insieme.

ReadOnlyCollection.

Codice

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; }        
        }    
    }
}

Consentire agli utenti di modificare una proprietà

Oggetto di descrizione

Potrebbe essere necessario consentire a un consumer della classe di modificare una proprietà. Nell'esempio riportato di seguito viene illustrata una proprietà in lettura e scrittura che viola questa regola.

Codice

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; }        
        }    
    }
}

Commenti

Nell'esempio seguente viene corretta la violazione modificando la proprietà in modo che restituisca un oggetto Collection.

Codice

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; }        
        }    
    }
}

Regole correlate

CA1024: Utilizzare proprietà dove appropriato