❔ ✅ Rolling back list state after exception while adding elements to it
In the following sharplab demo, I add elements to a list inside a try-catch block, and attempt to reset the list to its original state when an exception occurs. However, the state saved before adding is somehow also updated.
I feel like I'm missing something fundamental about deferred execution or try-catch blocks...
https://sharplab.io/#v2:C4LgTgrgdgPgAgJgIwFgBQcAMACOSAsA3OlrkgHQAyAllAI7FroBuAhmGZgM7YC82UAKYB3bDS7AAPHkwA+ABQBKbAG9sAIgDyYagHNarADbYk6gDQbteg8YTrsAX0Z4AnPIAk6gEKCAZgHswQRBsAG0VGXIAKX9aeXMNCxkuRQcAXXVFRnQAQQATPIBlYB0oXS55ZIsAcUFgYtLypSz0EiQ3ODhPHN9gQTAQ8MiYuISE5NS01rQAdQALAE9sPOo8qAByYGxgOcFsQ2oJbAlWPuwwf0NjACNWAGMAa2xaLlW9nb2707u57GvDfyPAD8mWyGCQAGZpEg5NhavUSrQmop0Cp0NgMWQkLgAOwafJ5QR5EzqRiYrG4/EFInYOxkzF4bFwPHqAk0iGk9GYnYXURCUQAOX8wAAkgBbAAOhkEYsEUD6eQAogAPO6CCXAaj+KBKRgOaZwfDYAkNJEVcRSGSyfaHYBJSHQ2HAfwElFoNFoclsDjXPyBPb8A4ScgAFX8Ft1XIxJQWUdU03J5ICQXuv0qMOOzyg2xdBTdicxHoLiaDwHIBPkKXpBf1nsxtfJX2AP2w8hVao1Wqg+cLcfJrniAB1s8bs4JVerNdrsIC7hAwEE8gBCYegvuY9cYgD0W+w8yWhw2Ww+NqOJzOFyuf3uT12QSBm9PW34vuTglD4dtkbrGNrDiAA
SharpLab
C#/VB/F# compiler playground.
25 Replies
The link doesn't work
yep, that works
Edited the URL inside the post
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
your code is heavily based in references
not values (copies)
But I thought classes were passed by reference by default?
They are
Ok so adding ref definitely works, I'm just puzzled why it's not the same as the default lol
Anyways, thanks @Henkypenky !
!close
i'm still baffled though
Closed!
Sharplab indicates, that
list = before.ToList()
is an unnecessary assignment.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?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);
...
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
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.this has nothing to do with that
He's asking why
ref string
is required when a string is a reference type?Where do you see
ref string
?
I see ref strings
, as in, a variable named strings
being passed as ref
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
?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
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
no, strings is not passed by ref unless you explicitly say ref
oh you were answering to Fused?
i'm lost
Huh, now that you say it, I'm lost too <a:CL0_PotatoSweat:727901137281482943> i was trying to clear up the original question
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?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. mbyeah 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
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.