On 5/11/22 9:06 PM, Christopher Katko wrote:

I just realized foreach copies by value by default. Maybe. Sometimes. When? I don't even know anymore. Because I "thought" I had to use foreach(ref) to be able to affect the original data structure (because by value means its a copy, right?). Except somehow everything has been working just fine, mutating the original structure/array from inside foreach and adding foreach(ref) makes no changes. Will it be faster? Maybe... who knows.

ref can be confusing depending on the type. foreach indeed does everything by value unless you specify ref. But some types "act like" a ref. Remember, the copy is a *shallow* copy. So a "copy" of an array slice, for example, still refers to the original elements.

-Steve

Reply via email to