help
Root Question Message
AddStrings(ref strings, GetStrings());
void AddStrings(ref List<string> list, IEnumerable<string> toAdd)
list = before.ToList()
is an unnecessary assignment.ref
to a reference type either, can someone explain?foo = "asdasd"
, you are not modifying the string. You are creating a new string altogether. Not sure about ref List
, though.ref string
is required when a string is a reference type?ref string
?ref strings
, as in, a variable named strings
being passed as ref
ref string
ref List
would be needed. I don't see how this would "pass by value" if it's a reference. Can you only change the actual items in the list, but you can't change the list itself unless you add ref
?strings
is passed as ref. Then in the method, you are adding stuff to list
, meaning you are adding stuff to strings
. But then you are killing the original ref for list
by doing list = before.ToString()
. strings
will still be a list with 5 elements, while list, now only in the scope of the function itself, only has 2 elements.ref
. Because you don't wanna have another reference, when you are reassigning list
strings
in the original code, is passed by value, it's reference is evaluated and a copy is generated, you can do whatever you want with that copy, it's an exact copy of strings
but only with the existing references, so new references are not part of the original object, so you either return the result and assign it or you do everything by refstrings
is gone. I just worded poorly. mb