C#
C#

help

HimmDawg
HimmDawg2/17/2023
The link doesn't work
HimmDawg
HimmDawg2/17/2023
yep, that works
JansthcirlU
JansthcirlU2/17/2023
Edited the URL inside the post
Henkypenky
Henkypenky2/17/2023
because list is a variable scoped inside the method?
Henkypenky
Henkypenky2/17/2023
and you return nothing from that method
Henkypenky
Henkypenky2/17/2023
so it only lives there
Henkypenky
Henkypenky2/17/2023
oh i see
Henkypenky
Henkypenky2/17/2023
you are gonna want to pass by ref in that case
Henkypenky
Henkypenky2/17/2023
AddStrings(ref strings, GetStrings());


void AddStrings(ref List<string> list, IEnumerable<string> toAdd)
Henkypenky
Henkypenky2/17/2023
your code is heavily based in references
Henkypenky
Henkypenky2/17/2023
not values (copies)
JansthcirlU
JansthcirlU2/17/2023
But I thought classes were passed by reference by default?
Angius
Angius2/17/2023
They are
JansthcirlU
JansthcirlU2/17/2023
Ok so adding ref definitely works, I'm just puzzled why it's not the same as the default lol
JansthcirlU
JansthcirlU2/17/2023
Anyways, thanks @246732334282440704 !
JansthcirlU
JansthcirlU2/17/2023
!close
Henkypenky
Henkypenky2/17/2023
i'm still baffled though
HimmDawg
HimmDawg2/17/2023
Sharplab indicates, that list = before.ToList() is an unnecessary assignment.
Florian Voß
Florian Voß2/17/2023
I don't understand why you have to add ref to a reference type either, can someone explain?
Florian Voß
Florian Voß2/17/2023
shouldn't it have no effect because its the default behaviour?
Henkypenky
Henkypenky2/17/2023
yeah you are still passing a reference but you are passing it by value
Henkypenky
Henkypenky2/17/2023
and the method evaluates that reference and generates a value at that moment
FusedQyou
FusedQyou2/17/2023
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
Henkypenky2/17/2023
this has nothing to do with that
FusedQyou
FusedQyou2/17/2023
He's asking why ref string is required when a string is a reference type?
Angius
Angius2/17/2023
Where do you see ref string?
Angius
Angius2/17/2023
I see ref strings, as in, a variable named strings being passed as ref
FusedQyou
FusedQyou2/17/2023
I misread this, I thought the first one was a ref string
FusedQyou
FusedQyou2/17/2023
Carry on
FusedQyou
FusedQyou2/17/2023
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
Henkypenky2/17/2023
again, the List is a reference type
Henkypenky
Henkypenky2/17/2023
but when you put it in the parameter
Henkypenky
Henkypenky2/17/2023
it's evaluated and it generates a copy of it
Henkypenky
Henkypenky2/17/2023
it's passed by value
HimmDawg
HimmDawg2/17/2023
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.
HimmDawg
HimmDawg2/17/2023
That's why you need ref. Because you don't wanna have another reference, when you are reassigning list
Henkypenky
Henkypenky2/17/2023
no, strings is not passed by ref unless you explicitly say ref
Henkypenky
Henkypenky2/17/2023
oh you were answering to Fused?
Henkypenky
Henkypenky2/17/2023
i'm lost
HimmDawg
HimmDawg2/17/2023
Huh, now that you say it, I'm lost too :CL0_PotatoSweat: i was trying to clear up the original question
Henkypenky
Henkypenky2/17/2023
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
Henkypenky
Henkypenky2/17/2023
does it make sense?
HimmDawg
HimmDawg2/17/2023
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
Henkypenky2/17/2023
yeah it's a very hard thing to understand sometimes, took me years xd
Henkypenky
Henkypenky2/17/2023
still don't get it
Henkypenky
Henkypenky2/17/2023
haha
Henkypenky
Henkypenky2/17/2023
pass by reference has it's uses
Henkypenky
Henkypenky2/17/2023
but i don't use it, like at all
Henkypenky
Henkypenky2/17/2023
i guess if the object is very large, and ref is an option, it's a good idea
ContactFrequently Asked QuestionsJoin The DiscordBugs & Feature RequestsTerms & Privacy