Jim Arnold wrote:

> but *not* this:

> Foo[] foos = new Foo[1];
> foos[0] = new Foo();

> foreach(Foo foo in foos) {
>     foo.Bar = "bar";
> }

> The compiler error is distinctly unhelpful: "The left-hand side of an
> assignment must be a variable, property or indexer" - which it plainly is.

>From the .NET docs on foreach:

   The foreach statement repeats a group of embedded statements for each
   element in an array or an object collection. The foreach statement is
   used to iterate through the collection to get the desired information,
   but should not be used to change the contents of the collection to avoid
   unpredictable side effects.

My guess is that the compiler is enforcing this behavior, just as it seems
to enforce the idea that you cannot reassign a reference that it declared
inside a "using" statement (which also looks like it should be valid by
looking just at the source).

Brad

--
Read my web log at http://www.quality.nu/dotnetguy/

You can read messages from the DOTNET archive, unsubscribe from DOTNET, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.

Reply via email to