Re: Newbie - delete item from collection
Simon Robinson <[email protected]>
| Newsgroups | gmane.comp.windows.devel.dotnet.advanced |
|---|---|
| Message-ID | <LISTSERV%[email protected]> |
I don't have a reference offhand, but I'm pretty sure I recall reading that taking out of loops the evaluation of expressions that the compiler can see are going to be constant through the loop is a C# compiler optimization, and has been ever since .NET 1.0. I'd imagine it's a bit of a moot point though since another compiler optimization is that very short properties (or methods etc.) will be inlined. I'd be astounded if the implementation of Array.Length wasn't simply to return the value of a length member field - that kind of property would certainly be inlined and so replaced by the (current, but constant) value of the length field in the compiled code wherever the property is invoked. That would mean that actually there's no 'evaluation' to take out of the loop anyway. (I'm pretty sure the same would be true for the (non-constant) Count property of lists, as per my other post). Simon On Tue, 18 Nov 2008 10:44:46 -0000, Geoff Taylor <[email protected]> wrote: >Hi, > >Thanks for that - it's good to know. Do you have a reference for this as a >general behaviour, or is it just a compiler-specific optimisation? > >Cheers, > > Geoff > >> -----Original Message----- >> From: Discussion of advanced .NET topics. [mailto:ADVANCED- >> [email protected]] On Behalf Of Hewitt, Simon C. (Contractor) >> Sent: 18 November 2008 10:43 >> To: [email protected] >> Subject: Re: [ADVANCED-DOTNET] Newbie - delete item from collection >> >> That's true for Arrays as the compiler 'knows' that .Length won't >> change >> during the enumeration. >> Everything else will have its Count re-evaluated during each loop. >> >> Cheers >> Simon >> >> -----Original Message----- >> From: Discussion of advanced .NET topics. >> [mailto:[email protected]] On Behalf Of Geoff Taylor >> Sent: 18 November 2008 10:20 >> To: [email protected] >> Subject: Re: [ADVANCED-DOTNET] Newbie - delete item from collection >> >> I know it's a property. Tsk. ;) >> >> My confusion was around it being whether it was invoked in the loop at >> all. >> I was under the impression - I'm sure I read this somewhere back in the >> .NET 1.0 or beta days - that a for loop like: >> >> for (int i = 0; i < list.Count; i++) >> { >> ... >> } >> >> Was created in the IL as: >> >> int temp = list.Count; >> for (int i = 0; i < temp; i++) >> { >> ... >> } >> >> See what I mean now? >> >> I can't find any reference to this behaviour though, and I've just read >> the relevant parts of the C# Language Specification and it's not there. >> I even wrote it a simple program in Snippet Compiler to check. So it >> looks like my understanding was hogswash. Sorry about that. >> >> Apologies for introducing confusion. Now, any idea where I got that >> notion? >> >> Cheers, >> >> Geoff >> >> > -----Original Message----- >> > From: Discussion of advanced .NET topics. [mailto:ADVANCED- >> > [email protected]] On Behalf Of Eddie Lascu >> > Sent: 17 November 2008 22:23 >> > To: [email protected] >> > Subject: Re: [ADVANCED-DOTNET] Newbie - delete item from collection >> > >> > Count is a property that returns the actual number of elements >> > contained in the ArrayList so has to be reevaluated every time is >> > invoked. >> > >> > -----Original Message----- >> > From: Discussion of advanced .NET topics. >> > [mailto:[email protected]] On Behalf Of Geoff >> > Taylor >> > Sent: Monday, November 17, 2008 5:06 PM >> > To: [email protected] >> > Subject: Re: [ADVANCED-DOTNET] Newbie - delete item from collection >> > >> > Really? I thought that was cached before the first iteration of the >> > loop and not re-evaluated. Does it really evaluated Count each time >> > through the loop? >> > >> > Geoff >> > >> > On 17 Nov 2008, at 21:04, Chris Anderson <[email protected]> wrote: >> > >> > >> (I'd also have expected it to throw an exception towards the end, >> > >> because there are no longer _someList.Count items in the list.) >> > > >> > > Yes there are ;-) >> > > _someList.Count will be decrementing as you remove items >> > > >> > > =================================== >> > > View archives and manage your subscription(s) at >> > > http://peach.ease.lsoft.com/archives >> > >> > =================================== >> > View archives and manage your subscription(s) at >> > http://peach.ease.lsoft.com/archives >> > >> > =================================== >> > View archives and manage your subscription(s) at >> > http://peach.ease.lsoft.com/archives >> >> =================================== >> View archives and manage your subscription(s) at >> http://peach.ease.lsoft.com/archives >> >> =================================== >> View archives and manage your subscription(s) at >> http://peach.ease.lsoft.com/archives > >=================================== >View archives and manage your subscription(s) at http://peach.ease.lsoft.com/archives =================================== View archives and manage your subscription(s) at http://peach.ease.lsoft.com/archives