Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Array.Reverse() versus Enumerable.Reverse<TSource>()

Tags:

.net

linq

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()?

like image 417
mbrownnyc Avatar asked Jul 19 '11 16:07

mbrownnyc


2 Answers

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.

like image 102
Andrey Avatar answered Oct 22 '22 01:10

Andrey


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().

like image 41
Daniel Hilgarth Avatar answered Oct 22 '22 02:10

Daniel Hilgarth