CA1819: Properties should not return arrays
Visual Studio 2012
|
TypeName |
PropertiesShouldNotReturnArrays |
|
CheckId |
CA1819 |
|
Category |
Microsoft.Performance |
|
Breaking Change |
Breaking |
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.
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(); } } }