I have some code that uses the Reverse extension method.
To make sure that it does not reverse the original list the original programmer used GetRange to create a new list.
dim a = New List(of Thing)
... fill in a
dim b = a.GetRange(0, a.count)
b.reverse()
Presumably GetRange guarantees to always create a new object. Does this apply to ToList as well?
Then I could write:
dim a = New List(of Thing)
... fill in a
dim b = a.ToList()
b.reverse()
The advantage then is that the type of a could be IEnumerable(of T) and it would still work.
I have tested this myself and it has always been true that b is a new object. The question is this:
Is it guaranteed that future implementations will behave the same or is this behaviour an accident of the implementation?
An obvious 'optimization' would be to return a reference to the original object if it were already a list, which would of course mean that the second version would reverse the original list.