tags:

views:

33

answers:

1

If I have a method that has a params parameter, can it be passed by reference and updated every time a yield is called.

Something like this:

public static void GetRowsIter(ref params valuesToUpdate)
{

    foreach(row in rows)
    {
       foreach(param in valuesToUpdate
       {
          GetValueForParam(param)
       }
       yield;
    }
}

Is that legal? (I am away from my compiler or I would just try it out.)

+1  A: 

No. params just creates an array that contains the parameters being passed. This array, like all others, is just a collection of variables, and it's not possible to declare a ref variable or array type. Because of this only actual explicit parameters can be passed as ref or out.

That being said, if the type is a reference type then it will exhibit reference type semantics as usual, meaning that any changes made to the object will be reflected in all code that has access to that reference. Only assignments to the actual variable would not be reflected.

However, I'm not certain exactly what your code is intended to do. The yield statement either has to be followed by the return statement and a value or by the break statement, which ends the iterator.

Adam Robinson
Thanks for the info. I was trying to code a way to return a variable amount of values via the yield statement. Looks like that is not going to be possible (at least not this way).
Vaccano
@Vaccano: You can certainly return a variable number of values, but they can't be `ref`.
Adam Robinson