My program intensively uses Reverse, e.g. Array.Reverse(myArray,3,5)
I would like my program could accept both array and List as input, so I go for IList
However, I couldn’t find an IList method which is the same as Reverse.
There is one extension method named Reverse, however it produces IEnumerable stream but not in-place rearrangement. (which I assume takes more copy time)
I thought of using cast, but was afraid that cast would be inefficient as well.
So, what should I do?
Worst case scenario, I make 2 program, 1 takes array, the other takes List, and then overloading?
OOP-way – make a wrapper, overload it dozen times:
Add an overload each time you need another collection-alike class to be reversed in such way. This approach relies on system internals, very effective and robust, but may be verbose in case you are willing to reverse many kinds of objects.
I-can-do-it-myself-better-way:
Just add range checks/preconditions/invariants/etc. Also, it might be inefficient with lists, as it requires random access to the contents of the list, but I think you can’t workaround it using “conventional weapons” (i.e. not using reflection and direct memory manipulation).
So, my suggestion – overloading is the way to go.