Nicholas Paldino [.NET/C# MVP] <mv*@spam.guard.caspershouse.com> wrote:
Actually, the value type is not read only.
I'm not sure what you mean by this, but the variable itself is
certainly read only - and if you were able to directly set any fields
within the struct normally, they would be read only too. Here's an
example:
using System;
struct Foo
{
public int x;
}
class Test
{
static void Main()
{
Foo[] f = new Foo[10];
foreach (Foo foo in f)
{
foo.x = 10;
foo = new Foo();
}
}
}
Both of the assignments fail - foo.x isn't classified as a variable
because foo is readonly, and the direct assignment fails for the same
reason.
From the spec:
<quote>
In either expansion, the enumerator variable is a temporary variable
that is inaccessible in, and invisible to, the embedded statement, and
the element variable is read-only in the embedded statement.
</quote>
Note the last phrase.
Your post gave the reason why if it *did* compile, it still wouldn't do
what Pohihihi wanted - but the above is why it didn't compile in the
first place.
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog:
http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too