Properties Retuning Arrays

J

Jon Skeet [C# MVP]

Cool Guy said:
Why, when a property returns an array, why doesn't it return a reference to
that array?

It does.
e.g.:

public string[] Array
{
get
{
return arr;
}
}

According to MSDN, a copy of the array is returned here. Why is this?

No, that's suggesting that it's a good idea to return a copy of the
array - otherwise the caller can effectively change internal state by
changing the contents of the array.
 
C

Cool Guy

Jon said:
No, that's suggesting that it's a good idea to return a copy of the
array - otherwise the caller can effectively change internal state by
changing the contents of the array.

Oh, of course. I just ran FxCop for the first time and it ended up
confusing me.

Thanks Jon.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top