I’ve been adapting to Java 1.5 and c# (and c#, with .NET 2.0), all of which feature a new construct: the foreach loop. The syntax is relatively similar to the construct in numerous other languages, such as perl’s:
1 2 3 4 5 |
|
In c# (or java, the syntax is the same), to loop over an array or IEnumerable collection, you just do:
1 2 3 4 5 |
|
Unfortunately, as I found today, the c# version shares something else with the Perl version: An absence of type checking. Despite the explicit types in the loop construct, and the presence of generics (although these are missing in .NET foreach in c# is not type safe. The end result is that you can do fun things like this with out the slightest hint of a warning:
1 2 3 4 5 6 7 |
|
I find this particularly vexing because if you simply re-write the foreach to be a standard for loop, then the type checker will jump in and complain about the loss of precision:
1 2 3 4 5 6 |
|
“`
foreach(T id in IEnumerable){ // body }
1 2 3 4 |
|
double[] values = new double[]{0.5, 0.6, 0.7};
for (int v : values){ // causes compile time error. System.out.println(“v=”+v); }
“` Note that it is actually an error in java, not a warning even. I would assume this has something to do with the requirements Sun placed on backwards compatability with older JREs – much, if not all, of the java Generics changes are simply rewrite rules. C#, however, has no such constraints and can take advantage of a number of performance improvements that are not available to the java compiler. I still don’t see that as an excuse for discarding type saftey. –ERC