开发者

Can params be used to pass variables by ref via a function using yield

开发者 https://www.devze.com 2023-01-05 22:00 出处:网络
If I 开发者_JAVA技巧have a method that has a params parameter, can it be passed by reference and updated every time a yield is called.

If I 开发者_JAVA技巧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.)


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号