because it is a 'foreach iteration variable'
ASK:
Hi,every one.
What is wrong with the following -
short[] arrfile = new short[100];
foreach (short s in arrfile)
{
s = s.trim();
}
Error 1 Cannot assign to 's' because it is a 'foreach iteration
variable'
Thanks.
Question:
You're trying to change the value of the item you're working on.
Just do
foreach (short s in arrfile)
{
string item =s.trim();
}
from MSDN:
"This error occurs when an assignment to variable occurs in a read-
only context. Read-only contexts include foreach iteration variables,
using variables, and fixed variables. To resolve this error, avoid
assignments to a statement variable in using blocks, foreach
statements, and fixed statements."
The foreach keyword just enumerates IEnumerable instances (getting an
IEnumerator instances by calling the GetEnumerator() method).
IEnumerator is read-only, therefore values can't be changed using
IEnumerator =can't be changed using the foreach context.
Hope this helps.