Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All
Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here.

Properties should not return arrays

TypeName

PropertiesShouldNotReturnArrays

CheckId

CA1819

Category

Microsoft.Performance

Breaking Change

Breaking

A public or protected property in a public type returns an array.

Arrays returned by properties are not write-protected, even if the property is read-only. To keep the array tamper-proof, the property must return a copy of the array. Typically, users will not understand the adverse performance implications of calling such a property. Specifically, they might use the property as an indexed property.

To fix a violation of this rule, either make the property a method or change the property to return a collection.

Attributes can contain properties that return arrays, but cannot contain properties that return collections. You can suppress a warning that is raised for a property of an attribute that is derived from the [System.Attribute] class. Otherwise, do not suppress a warning from this rule.

Description

The following example shows a property that violates this rule.

Code

using System; 

namespace PerformanceLibrary
{    
    public class Book    
    {        
        private string[] _Pages;      

        public Book(string[] pages)        
        {            
            _Pages = pages;        
        }         

        public string[] Pages        
        {            
            get { return _Pages; }        
        }    
    }
}

Comments

To fix a violation of this rule, either make the property a method or change the property to return a collection instead of an array.

Description

The following example fixes the violation by changing the property to a method.

Code

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

Description

The following example fixes the violation by changing the property to return a

ReadOnlyCollection.

Code

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

Description

You might want to allow the consumer of the class to modify a property. The following example shows a read/write property that violates this rule.

Code

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

Comments

The following example fixes the violation by changing the property to return a Collection.

Code

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

Community Additions

ADD
Show:
© 2015 Microsoft