Cloning an array is a memory allocation; the effect will depend directly on the size of the array.
An indexer is just a method; Another approach could be to simply add a method such as GetName(int index) and GetValue(int index) (if, for example, your arrays are names / values). C # has no built-in named indexers.
If performance is important and you need to access multiple values, then another approach is to have a method that copies the values ββto the array that the calling object provides, for example GetNames(string[] names, int offset) { this.names.CopyTo(names, offset); } GetNames(string[] names, int offset) { this.names.CopyTo(names, offset); } . This can be useful by allowing the caller to allocate one buffer and then use it to get values ββfrom several objects - or if you add several parameters (for a counter, etc.) to get values ββin batches, and not separately. However, this is necessary / useful, depending on the specific scenario.
source share