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

回眸只為那壹抹淺笑 提交于 2019-12-10 15:57:08

问题


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:


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.



来源:https://stackoverflow.com/questions/3176884/can-params-be-used-to-pass-variables-by-ref-via-a-function-using-yield

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!