Properties Retuning Arrays

  • Thread starter Thread starter Cool Guy
  • Start date Start date
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.
 
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.
 
Back
Top