I have a modification method with a signature like
private bool Modify(ref MyClass obj);
which will make changes to obj and indicate whether it will be returned. Modify does not reassign the link (I know this will not work), just changing the instance fields, so I want to use it to do the following:
foreach(MyClass obj in myList) { bool success = Modify(obj);
I ran into a compilation problem because obj not passed with the ref keyword. However, if I put the ref keyword like this:
bool success = Modify(ref obj);
I get “I can’t use obj as ref / out because it is a“ foreach iteration variable. ”I understand that foreach uses an immutable iterator and why this does not work.
My question is, what is the easiest alternative to do something like this work?
I tried using
foreach(int i = 0; i < myList.Count; i++) { bool success = Modify(ref myList[i]);
but they I get: "a property or index cannot be passed as a parameter to ref".
Thanks for the help.
source share