25 Replies
HimmDawg
HimmDawg2y ago
The link doesn't work yep, that works
JansthcirlU
JansthcirlUOP2y ago
Edited the URL inside the post
Henkypenky
Henkypenky2y ago
because list is a variable scoped inside the method? and you return nothing from that method so it only lives there oh i see you are gonna want to pass by ref in that case
AddStrings(ref strings, GetStrings());
AddStrings(ref strings, GetStrings());
void AddStrings(ref List<string> list, IEnumerable<string> toAdd)
void AddStrings(ref List<string> list, IEnumerable<string> toAdd)
your code is heavily based in references not values (copies)
JansthcirlU
JansthcirlUOP2y ago
But I thought classes were passed by reference by default?
Angius
Angius2y ago
They are
JansthcirlU
JansthcirlUOP2y ago
Ok so adding ref definitely works, I'm just puzzled why it's not the same as the default lol Anyways, thanks @Henkypenky ! !close
Henkypenky
Henkypenky2y ago
i'm still baffled though
Accord
Accord2y ago
Closed!
HimmDawg
HimmDawg2y ago
Sharplab indicates, that list = before.ToList() is an unnecessary assignment.
Florian Voß
Florian Voß2y ago
I don't understand why you have to add ref to a reference type either, can someone explain? shouldn't it have no effect because its the default behaviour?
Angius
Angius2y ago
Stack Overflow
List passed by ref - help me explain this behaviour
Take a look at the following program: class Test { List<int> myList = new List<int>(); public void TestMethod() { myList.Add(100); myList.Add(50); ...
Henkypenky
Henkypenky2y ago
yeah you are still passing a reference but you are passing it by value and the method evaluates that reference and generates a value at that moment
FusedQyou
FusedQyou2y ago
Strings are immutable. So when you do foo = "asdasd", you are not modifying the string. You are creating a new string altogether. Not sure about ref List, though.
Henkypenky
Henkypenky2y ago
this has nothing to do with that
FusedQyou
FusedQyou2y ago
He's asking why ref string is required when a string is a reference type?
Angius
Angius2y ago
Where do you see ref string? I see ref strings, as in, a variable named strings being passed as ref
FusedQyou
FusedQyou2y ago
I misread this, I thought the first one was a ref string Carry on Still makes me wonder why 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?
Henkypenky
Henkypenky2y ago
again, the List is a reference type but when you put it in the parameter it's evaluated and it generates a copy of it it's passed by value
HimmDawg
HimmDawg2y ago
No. 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. That's why you need ref. Because you don't wanna have another reference, when you are reassigning list
Henkypenky
Henkypenky2y ago
no, strings is not passed by ref unless you explicitly say ref oh you were answering to Fused? i'm lost
HimmDawg
HimmDawg2y ago
Huh, now that you say it, I'm lost too <a:CL0_PotatoSweat:727901137281482943> i was trying to clear up the original question
Henkypenky
Henkypenky2y ago
you are on the right track with your explanation. so to sum up 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 ref does it make sense?
HimmDawg
HimmDawg2y ago
Ahhh, i think i understand. Yeah, the reference itself is passed by value as you said. true. you can override that and the link to strings is gone. I just worded poorly. mb
Henkypenky
Henkypenky2y ago
yeah it's a very hard thing to understand sometimes, took me years xd still don't get it haha pass by reference has it's uses but i don't use it, like at all i guess if the object is very large, and ref is an option, it's a good idea no copy is generated hence no memory allocations
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server