Error while convertiong enhanced for loop with java to c# -
i try have enhanced loop of java code in c# code:
for (float value : array) { if (float.isinfinite(value) || float.isnan(value)) { value = 0; } }
i tried out:
foreach (float value in array) { if (float.isinfinity(value) || float.isnan(value)) { value = 0; } }
but have error told me have not right modify value because iteration variable.
you need use normal loop if want update iterator variable:
for(int = 0 ; < array.length; i++) { if (float.isinfinity(array[i]) || float.isnan(array[i])) { array[i] = 0; } }
from c# specifications:
the iteration variable corresponds read-only local variable scope extends on embedded statement. during execution of foreach statement, iteration variable represents collection element iteration being performed. compile-time error occurs if embedded statement attempts modify iteration variable (via assignment or ++ , -- operators) or pass iteration variable ref or out parameter.
Comments
Post a Comment