开发者

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

开发者 https://www.devze.com 2023-03-21 16:45 出处:网络
I have a single dimensional array of items declared and initialized as: string[] SubDirectorylist = Directory.GetDirectories(TargetDirectory开发者_如何转开发);

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消