|
-
April 26th, 2006, 05:43 PM
#1
foreach backward?
Does anyone know if there is a counterpart to the foreach statement?
For example:
foreach(BasePage page in middlePages) allows one to move sequentially forward through all pages in middlePages from the first page to the last.
However, I'd like also to be able to move backward through all the pages in middlePages, but from the last to the first page.
Is there a statement in C# that allows this or is there some way to fenagle foreach to do this for me?
Thanks
-Mike
-
April 27th, 2006, 01:59 AM
#2
-
April 27th, 2006, 07:54 AM
#3
Re: foreach backward?
From a personal perspective, foreach is to process a non-empty set's elements, and the cardinal order for output is not naturally determined beforehand in users'mind, you can look at what is inside foreach's brackets and imagine your output order is from behind or backward. It is a little odd. A more natural way of thinking about it is as random processing which quite fits our mind but would complicate the procedure.
I like your reverse order code snip anyway. That's a very nice idea...
Emiene Vous
-
April 27th, 2006, 08:16 AM
#4
Re: foreach backward?
 Originally Posted by Emiene
From a personal perspective, foreach is to process a non-empty set's elements, and the cardinal order for output is not naturally determined beforehand in users'mind, you can look at what is inside foreach's brackets and imagine your output order is from behind or backward. It is a little odd. A more natural way of thinking about it is as random processing which quite fits our mind but would complicate the procedure.
I like your reverse order code snip anyway. That's a very nice idea...
Well, foreach can be done on any type of collection, ordered, sorted, or non-ordered.
In C# 2.0 multiple iterators are supported, for instance look at this example: http://msdn2.microsoft.com/en-US/library/ee5kxzk0.aspx
In C# 1.1 a class can have only one iterator that can be used by the for each statement, somethin like the reversor can be used (I too liked it). An alternative that I have used for some collections is to create inner classes with the sole purpose of providing alternate iterators. A 3rd solution is to implement your own iterators and use them the classic way in a while loop. In most cases though I use boudino's first solution.
-
April 27th, 2006, 08:38 AM
#5
Re: foreach backward?
 Originally Posted by klintan
Well, foreach can be done on any type of collection, ordered, sorted, or non-ordered.
In C# 2.0 multiple iterators are supported, for instance look at this example: http://msdn2.microsoft.com/en-US/library/ee5kxzk0.aspx
In C# 1.1 a class can have only one iterator that can be used by the for each statement, somethin like the reversor can be used (I too liked it). An alternative that I have used for some collections is to create inner classes with the sole purpose of providing alternate iterators. A 3rd solution is to implement your own iterators and use them the classic way in a while loop. In most cases though I use boudino's first solution.
Yes, I am completely agree that they can be accomplished with the use of generic code and have no denying for the fact that they are user-defined functions to fit their personal purpose in this way or that.
Emiene Vous
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|