Federico Raggi wrote:

> Interestingly, if you define Foo as a class instead of a struct then Foo
> [0].Bar can be modified and has the correct "bar" value after the loop. If
> Foo is an struct it doesn't.

That's because in the direct array access code, the structs are being
directly modified. In the case of foreach, a copy of the struct is made.
Modifying the copy doesn't do you any good.

When you use a class, of course, then foreach just brings out a new
reference to the object in the array (which makes your modifications
"stick").

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