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

If I have a method with params parameter, it can be passed by reference and updated every time the income is called.

Something like that:

 public static void GetRowsIter(ref params valuesToUpdate) { foreach(row in rows) { foreach(param in valuesToUpdate { GetValueForParam(param) } yield; } } 

Is it legal? (I'm away from my compiler, or I'll just try it.)

+4
source share
1 answer

Not. params simply creates an array containing the parameters to pass. This array, like everyone else, is just a collection of variables, and it is impossible to declare a variable of type or t in the ref array. Because of this, only real explicit parameters can be passed as ref or out .

Moreover, if the type is a reference type, it will demonstrate the semantics of the reference type, as usual, which means that any changes made to the object will be reflected in all the code that has access to this link. Only assignments to the actual variable are not reflected.

However, I'm not sure exactly what your code should do. The yield should be followed by a return and the value or break statement that completes the iterator.

+6
source

Source: https://habr.com/ru/post/1314693/


All Articles