Click here to Skip to main content
15,889,335 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello , i have a question about CA1819 msdn performance warning.

The rule is:
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.

Is this rule valid for other objects too, or it is only for arrays, and if it's only for arrays why?

Thanks in advance.
Kriss
Posted
Updated 9-Sep-13 2:55am
v2

1 solution

That warning is only for arrays. It's telling you that since the property is not ReadOnyl that there is nothing preventing another coder from replacing your classes array with another one that it isn't maintaining.

Other object types have their own messages.
 
Share this answer
 
Comments
Kriss32 9-Sep-13 9:17am    
Thanks for the answer,but that rule is applicable either when the property is read only.
I want to know if other objects are mutable too when returned by the property,or this only happens for the arrays?
Kriss32 9-Sep-13 10:55am    
Why does this happen only with arrays???
Dave Kreskowiak 9-Sep-13 13:16pm    
Because of the unique nature of arrays. The problem comes in because you can either replace the array or you can replace items in the array, completely out of the control of the Property exposing it.
Kriss32 10-Sep-13 3:40am    
What is this unique nature? Is there any specific reason why only arrays can be modified? Other objects like string,list, IDbCommand etc. are not mutable. I think there is something that makes arrays mutable and other objects immutable, and that's what i am asking, do anyone know that reason?
Dave Kreskowiak 10-Sep-13 7:59am    
Because you cannot make your own version of an array where the individual elements cannot be replaced!

You can make your classes Property that exposes the array ReadOnly. This prevents code from replacing the array that is under the classes control, but you cannot protect the elements of the array itself, removing the classes control over that data. You also cannot create your own version of Array that provides ReadOnly elements.

When code outside of the class gets the array being returned, it can modify the elements of the array without the class knowing about it. That is a data integrity problem, and possibly a security issue, depending on what the data in the array is used for.

To get around like CodeAnalysis message, you'd have to wrap the array in a class that exposes the elements of the array in a controlled manner, or don't expose the array itself as a property and provide methods in the class to read and write individual elements and another to return a COPY of the array if the caller needs the entire dataset.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900