To clarify I’m doing this in Unity3D, which may or may not be important?
I’m trying to figure out if I can pass a value by ref to an IEnumerator function that does not yield. If I try to do it with one that yields, VS2010 complains (“Iterators cannot have ref or out parameters”), but, if I wrap the call up with a similar IEnumerator function that calls the yielding function, but does not yield itself, the error goes away and things appear to work. I’m trying to find out if I’m in unexpected behavior land or if this is normal behavior.
Here’s an example of what I’m doing:
IEnumerator Wrapper(ref int value)
{
int tmp = ++value; // This is the ONLY place I want the value
return Foo(tmp); // of the ref parameter to change!
} // I do _NOT_ want the value of the ref
// parameter to change in Foo()!
IENumerator Foo(int value)
{
// blah blah
someFunc(value);
someSlowFunc();
yield return null;
yield return null;
}
Looks good. The top function just returns an IEnumerator – but is otherwise a normal function. The bottom function is an IEnumerator [transformed into a funky class by the compiler] and as such cannot have a ref value.
The top function could have been written as such:
This is a little more messy – but it shows how this is a normal function that deals with two pieces of data. A int passed by referance, and a IEnumerator [just a class] that it returns [in this example by using out].
Supplemental: This is how stuff works behind the scenes: