Array.Reverse() versus Enumerable.Reverse<TSource>()
I have a single dimensional array of items declared and initialized as:
string[] SubDirectorylist = Directory.GetDirectories(TargetDirectory开发者_如何转开发);
I'd like to reverse the members and found Enumerable.Reverse<TSource>()
.
What advantages, if any, does LINQ's implementation of reverse have over Array.Reverse()
?
Beside what Daniel told System.Linq.Enumerable.Reverse()
actually creates a copy then iterates it from end to beginning, when System.Array.Reverse()
does in place transformation.
System.Linq.Enumerable.Reverse()
works on all classes that implement IEnumerable
where as System.Array.Reverse()
only works on arrays. If you know that you have an array, there is nothing wrong with using System.Array.Reverse()
.
If Performance matters to you, use Array.Reverse()
.
System.Linq.Enumerable.Reverse()
has the advantage of working on every IEnumerable like Arrays, Lists and so on.
精彩评论