I've got a simple function that takes a List parameter. While working with it, it copies it and reverses the copy using .NET's List(Of T).Reverse method.
Private Function FindThing(ByVal Things As List(Of Thing)) As Thing
Dim ReverseOrderThings As List(Of Thing) = Things
ReverseOrderThings.Reverse()
For Each t As Thing In ReverseOrderThings
...
Next
Return Nothing
End Function
My parameter is defined as ByVal. But, it seems that ByVal does not prevent the code in the procedure from changing the list's order.
Dim Things As List(Of Thing) = GetSortedListFromSomewhere()
Dim FoundThing As Thing = FindThing(Things)
For Each t As Thing In Things
...
'OMG! My Things are in reverse order!!1! WTF?'
Next
How can I protect my list parameter in such a function?
Update:
This is the correct way to make a copy of the list.
Dim ReverseOrderThings As List(Of Thing) = New List(Of Thing)(Things)
Now the list passed as a parameter is unaffected by the Reverse() method.