Changing a stuct inside another struct in a foreach loop?

The reason you are seeing this behavior has to due with using an iteration variable. Iteration variables are read-only in the sense that in C# you cannot modify them (C# lang spec section 8.8.4 details this.

The reason you are seeing this behavior has to due with using an iteration variable. Iteration variables are read-only in the sense that in C# you cannot modify them (C# lang spec section 8.8.4 details this The iteration variable corresponds to a read-only local variable with a scope that extends over the embedded statement Playing with read-only mutable structs is a path to unexpected behavior. Instead of using the variable directly you are actually using a copy of the variable.

Hence it's the copy that is getting incremented in the case of myStruct and not the actual value. This is why the original value remains unchanged. Eric did a rather in depth article on this topic that you can access here blogs.msdn.com/ericlippert/archive/2008/... Yet another reason why you should always have immutable structs.

If myStruct is readonly (which I'd expect it to be) then surely myStruct.InnerCopy.AddOne() should fail by throwing an exception. If you can call a non-const method (as we'd call it in the C++ world) on a const / read-only object and have it silently do nothing rather than complain, there's surely something very wrong... – AAT Nov 6 '09 at 16:03 AAT, what happens here is that the mutating method is called on a mutable copy. Value types are COPIED BY VALUE, that's why they're called "value" types.

If you don't want stuff to be copied by value, then don't use value types. – Eric Lippert Nov 6 '09 at 16:11 1 @AAT C#'s read-only and the C++ concept of const are very different constructs.In C++ it's an attempt to ensure a single value is not unexpectedly mutated. In C# read-only when applied to value types causes a copy of the value to be created whenever it is used (or at least when a member is accessed) thus preventing mutation of the original value.

I'm hesitant to say much further because I don't deeply understand all of the circumstances under which C# ensures of value of the struct is returned. Hopefully Eric will be along in a bit to give a more thorough answer. – JaredPar Nov 6 '09 at 16:14 2 Indeed, Jared, your explanation is correct.

Unfortunately, we do not strictly ensure that so-called "read only variables" like the "foreach" and "using" variables eare treated exactly the same way that we treat readonly fields; it is possible to abuse certain compiler behaviours (involving closure rewriting) to induce mutations in such variables of mutable value types. I consider these behaviours to be compiler bugs, but the scenarios are obscure and the spec is imprecise. Hopefully we'll fix those up in a hypothetical future release.

– Eric Lippert Nov 6 '09 at 16:14.

I think you'll find that the MyStruct. InnerStruct actually returns a copy of the struct, and not the struct itself. Therefore you're increasing the value of the copy... Sure I read a blog about this recently somewhere.

If you change the following myStruct.innerStruct.AddOne(); Console. WriteLine(myStruct.innerStruct. Counter); to MyInnerStruct inner = myStruct.

InnerStruct; inner.AddOne(); Console. WriteLine(inner. Counter); Then you should see it start working.

My best guess is that is has something to do with myStruct being readonly. – TOS Nov 6 '09 at 15:18.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions