本文介绍了我该怎么用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 大家好, 想象一下我有一个int数组: int [] anArray = new int [.. ]; 我想提取所有优于500的整数 我能做到: var result = new List< int>(); foreach(int in aArray) if(i 500) result.Add(i); 或 var resultFind = Array.FindAll(anArray,elt = elt 500); 或 var resultWhere = anArray.Where(elt = elt 500); 或 var resultLinq =来自i in aArray,其中我500选择i; 最好的方法是什么? 感谢您的回答 Hi all, Imagine I''ve an array of int : int[] anArray = new int[..]; I want to extract all the integer that are superior to 500 I can do : var result = new List<int>();foreach (int i in anArray)if (i 500)result.Add(i); or var resultFind = Array.FindAll(anArray, elt =elt 500); or var resultWhere = anArray.Where(elt =elt 500); or var resultLinq = from i in anArray where i 500 select i; What is the best way ? Thanks for your answer推荐答案 方法#1和#2创建新的收藏品,#3和#4没有(事实上他们确实没有创造任何东西;只有在评估时才会产生新的序列。 方法#3和#4使用LINQ(因此需要.NET 3.5),方法#1和#2做 $ b $不是。可读性是主观的,但#1明显不如 其他三种方法。 因此答案是可预测的它取决于。一般来说,我会选择 在上下文中哪一方最方便(我需要一个List,一个数组,一个IEnumerable吗?)并在转换时进行分析结果或其他 情况另有规定。孤立地说,这是一个太小的问题, 选择最佳方式。 - J. Methods #1 and #2 create new collections, #3 and #4 do not (in fact they donot create anything; they will yield a new sequence only when evaluated).Methods #3 and #4 use LINQ (and thus require .NET 3.5), methods #1 and #2 donot. Readability is subjective, but #1 is obviously less direct than theother three methods. The answer is therefore predictably "it depends". In general, I''d pickwhichever one was most convenient in the context (do I need a List, anArray, an IEnumerable?) and switch if profiling results or othercircumstances dictated otherwise. in isolation, it''s too small a problem topick the "best way". --J. 请注意,这表示最佳 ==最快。我个人会选择 最可读的方法。对我来说,那是: anArrayWhere(elt = elt 500); 我只会开始担心性能我有一些证据表明 非常重要。 - Jon Skeet - < sk *** @ pobox.com> 网站: http:// www。 pobox.com/~skeet 博客: http: //www.msmvps.com/jon_skeet C#深度: http ://csharpindepth.com Note that that suggests that "best" == "fastest". Personally I''d pickthe means which is the most readable. For me, that''s: anArrayWhere(elt =elt 500); I''d only start worrying about performance when I had some evidence thatit was really significant. --Jon Skeet - <sk***@pobox.com>Web site: http://www.pobox.com/~skeetBlog: http://www.msmvps.com/jon_skeetC# in Depth: http://csharpindepth.com 这篇关于我该怎么用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-17 17:49