4)你真的需要i,还是只需要元素?如果是后者, 请考虑使用foreach。 - Jon Skeet - < sk * **@pobox.com> http://www.pobox.com/ ~Sibet 博客: http://www.msmvps.com/jon。双向飞碟 如果回复小组,请不要给我发邮件 MuZZy写道: 考虑一下: ArrayList al = new ArrayList(); FillList(al); ///现在两个场景: /// 1. for(int i = 0; i< al.Count; i ++) {..do whatever ..} /// 2. int iCount = al.Count; for(int i = 0; i< iCount; i ++) {..do whatever ..} 是的,因为你已经发现我的问题是关于preffered 场景。我记得在Delphi Pascal的日子里,你应该像第一个属性那样预先设置第二个场景。每个循环都会请求Count. C#中的任何内容都有变化并且管理好了C ++还是它仍然提供使用场景2? 谢谢 MuZZy 它已经改变了,它是方案(1)当您使用索引器访问ArrayList中的项目时,。通过使用al.Count将没有运行时 检查:-) 见 http://tinyurl.com/3e9n3 ,部分范围检查消除 此外,当你有一个IEnumerable(一个ArrayList是)和 IEnumerable是*不是* System.Array,这是好的: foreach(在myList中的OBJECT o) { } HTH, Andy - 发送电子邮件我直接删除下面的* NO * SPAM *部分: *NO*SPAM*xmen40@*NO*SPAM*gmx.net Jon Skeet [C#MVP]写道: MuZZy< tn*@newsgroups.nospam>写道:考虑一下: ArrayList al = new ArrayList(); FillList(al); /// NOW TWO SCENARIOS: /// 1. for(int i = 0; i< al.Count; i ++) {..do whatever ..} /// 2. int iCount = al.Count; for(int i = 0; i< iCount; i ++) {..do whatever ..} 是的,因为你已经发现我的问题是关于preffered 场景。我记得在Delphi Pascal的日子里,你应该像第一个属性那样预先设置第二个场景。每个循环都会请求Count. C#中的任何内容都有变化并且管理好了C ++还是它仍然可以使用场景2? 1)无论如何,该属性几乎肯定会内联 2)性能差异,即使有一个是非常,非常,不可能做出任何可能性 3)可读性差异更为显着,IMO 4)你真的需要我,或只是元素?如果是后者,请考虑使用foreach。 感谢您的回复Jon! 让我们想象一下,我需要在循环中使用''我',或者它是C ++所以我不需要'b $ b'不能使用''foreach''。 现在,编译器内联属性如果不能保证 它不会在循环中改变? Hi, Consider this: ArrayList al = new ArrayList();FillList(al);/// NOW TWO SCENARIOS: /// 1.for (int i = 0 ; i < al.Count ; i++){..do whatever..} /// 2.int iCount = al.Count;for (int i = 0 ; i < iCount ; i++){..do whatever..}Yes, as you have already figured out my question is about prefferedscenario. I remember form the days of Delphi Pascal that you shouldpreffer second scenario as in the first property Count will be requestedon every loop. Did anything change in C# and managed C++ or it''s still proffered to usescenario 2? Thank youMuZZy 解决方案 MuZZy <tn*@newsgroups.nospam> wrote: Consider this: ArrayList al = new ArrayList(); FillList(al); /// NOW TWO SCENARIOS: /// 1. for (int i = 0 ; i < al.Count ; i++) {..do whatever..} /// 2. int iCount = al.Count; for (int i = 0 ; i < iCount ; i++) {..do whatever..} Yes, as you have already figured out my question is about preffered scenario. I remember form the days of Delphi Pascal that you should preffer second scenario as in the first property Count will be requested on every loop. Did anything change in C# and managed C++ or it''s still proffered to use scenario 2? 1) The property will almost certainly be inlined anyway2) The performance difference even if there is one is very, veryunlikely to make any odds3) The readability difference is much more significant, IMO4) Do you actually need "i", or just the element? If it''s the latter,consider using foreach instead. --Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeetIf replying to the group, please do not mail me tooMuZZy wrote: Hi, Consider this: ArrayList al = new ArrayList(); FillList(al); /// NOW TWO SCENARIOS: /// 1. for (int i = 0 ; i < al.Count ; i++) {..do whatever..} /// 2. int iCount = al.Count; for (int i = 0 ; i < iCount ; i++) {..do whatever..} Yes, as you have already figured out my question is about preffered scenario. I remember form the days of Delphi Pascal that you should preffer second scenario as in the first property Count will be requested on every loop. Did anything change in C# and managed C++ or it''s still proffered to use scenario 2? Thank you MuZZyIt has changed, it is scenario (1) when you use the indexer to accessthe items in the ArrayList. By using al.Count there will be no runtimecheck :-)see http://tinyurl.com/3e9n3, part "Range Check Elimination" Also, when you have an IEnumerable (which an ArrayList is) and theIEnumerable is *not* an System.Array, this is as good:foreach(OBJECT o in myList){}HTH,Andy--To email me directly, please remove the *NO*SPAM* parts below:*NO*SPAM*xmen40@*NO*SPAM*gmx.netJon Skeet [C# MVP] wrote: MuZZy <tn*@newsgroups.nospam> wrote: Consider this: ArrayList al = new ArrayList(); FillList(al); /// NOW TWO SCENARIOS: /// 1. for (int i = 0 ; i < al.Count ; i++) {..do whatever..} /// 2. int iCount = al.Count; for (int i = 0 ; i < iCount ; i++) {..do whatever..} Yes, as you have already figured out my question is about preffered scenario. I remember form the days of Delphi Pascal that you should preffer second scenario as in the first property Count will be requested on every loop. Did anything change in C# and managed C++ or it''s still proffered to use scenario 2? 1) The property will almost certainly be inlined anyway 2) The performance difference even if there is one is very, very unlikely to make any odds 3) The readability difference is much more significant, IMO 4) Do you actually need "i", or just the element? If it''s the latter, consider using foreach instead. Thanks for your reply Jon! Let''s imagine that i need ''i'' inside the loop, or that it''s C++ so ican''t use ''foreach''.Now, how does compiler inline property if there is no guarantee thatit''s not going to change inside the loop? 这篇关于关于表现的另一个问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-20 19:31