|
| | #4 (permalink) |
| Math Enthusiast/Badass MC Join Date: Jun 2002 Location: Seattle
Posts: 585
| Like a lot of others (probably) I went about trying to be clever with a way to do it in C# but there honestly isn't one that makes sense or is at all efficient. To access the iterator you should use for because foreach isn't made for that. |
| | |
| | #5 (permalink) |
| Registered User Join Date: Oct 2004
Posts: 1,567
| Here are two proof of concepts in one. Ontopic one first: All foreach needs is an object that implements IEnumberable. This means you are never guaranteed the object is indexable, or even know how many iterations will take place before the loop starts. As an example here is a program that uses foreach to write numbers from an object that just generates random numbers. The foreach will randomly terminate when a random number <= 20 is generated. There could be actual data structures where indexes don't make sense either. Second thing this code shows: The way I generate a series of random numbers is actually not random at all. Creating Random objects real close to each other will actually use the same seed and you will get a sequence of the exact same numbers if using the same range. So say you have a class for mobs that start with semi random stats. If they all create their own random number generator they will be a high chance they will start with the same stats. The correct way to do it is use a static random object (the commented out way) This way a new seed is used for each new random number since the .Next method generates a new seed. Code:
|
| | |
| | #6 (permalink) |
| euro scum Join Date: Aug 2002 Location: Sweden
Posts: 693
+1 Internets | Like Hachima so nicely showed, a Iterator can not give you a index nor can it find a specific object at a certain index. The reason is simple. Iterator is just a object that goes through a collection to give you a "next" object. It's supposed to work on all collections, even randomAccessCollections, Sets etc, where the order is irrelevant, it can't give you a index since that would make the method or function invalid for other type of collections. Foreach loop uses a iterator, so you're not supposed to get the index when using a for each. If you want the index, use a normal for loop, since that is your own implementation. |
| | |
| | #7 (permalink) | |
| Math Enthusiast/Badass MC Join Date: Jun 2002 Location: Seattle
Posts: 585
| Quote:
| |
| | |
| | #8 (permalink) |
| Registered User Join Date: Feb 2006
Posts: 1,560
| In my intro C# CS classes I found that one of the most pervasive mistakes was people who overused foreach, even people who were not new to programming. A lot of people would use a foreach loop to enumerate things like elements of an array, just because it was a handy shortcut to getting a reference to each item, in situations where they actually needed to get each element in order by index. Often, it would be an invisible bug, because the code would work properly. Most of Microsoft's collections like that will return their elements in order if you enumerate them with a foreach. It's awful practice though because there's no guarantee that they will enumerate them the same way twice; in the next version of the .NET framework your favorite foreach might spit out items in a different order and break your code. Not good. |
| | |
| | #9 (permalink) | |
| euro scum Join Date: Aug 2002 Location: Sweden
Posts: 693
+1 Internets | Quote:
Last edited by slitz : 12-04-2007 at 02:45 PM. | |
| | |
![]() |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
| |