I have two private arrays in a class that runs a certain operation using a method. After this method is called, those two arrays are filled with the results. It’s not a good practice to make those arrays properties, so I was planning to have a separate property that returns a clone of the private array.
1) What is the overhead of returning a clone of the array? Is it unnoticeable in every case?
2) I could use an indexer if there was just one array. Is there a specific mechanism to use indexers for more than one array in a class?
I think you mean “overhead” or “cost” and not “overload”. Anyway, computationally it’s
O(1)so it depends on the size of tha rray, but generally speaking Array copying is a cheap operation if it’s under a thousand elements or so.If you don’t intend on the arrays to be modified then you could expose your private arrays by wrapping them in a
ReadOnlyCollection<T>like so: