JansthcirlU
❔ ✅ 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
54 replies
❔ ✅ Using generics that implement IEqualityOperators results in ambiguous call to the == operator
If I have some
T: IEqualityOperators<T, T, bool>
and use a LINQ query such as myTs.FirstOrDefault(t => t == otherT)
, I get an error saying that the ==
operator call is ambiguous. How can I make sure the equality check happens through the IEqualityOperators
interface and not using the reference equals operator?
Sharplab: https://sharplab.io/#v2:C4LgTgrgdgNAJiA1AHwAICYAMBYAUBgRj1UwAJ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simulation or game loop using async-await and events [Answered]
I'm trying to simulate a system with participants where the system periodically notifies the participants to perform an action. I'm trying to use async/await with a custom
Task
delegate but exceptions aren't handled the way I thought they would be. Please refer to the Sharplab demo URL for more information: https://pastebin.com/J55Xzb6H.
I have an exception for Id == 5
which does not get caught. If I uncomment the next < score
exception it always gets caught for the listener with the biggest Id
(i.e. the last one to be added to the list).
Is there a better approach to do this? I wanted to leverage async/await to prevent having to wait for all the listeners to perform their actions one at a time but I do want to handle exceptions correctly.
Full Sharplab URL:
https://sharplab.io/#v2:C4LgTgrgdgNAJiA1AHwMRQgG0wQwEaYCmABIVPkQLABQAAgEwCMNtADMbYwCwDcL7nAKx9qLAMwd6HRgHYaAbxrFlHCZwBsHABwdNAWRwBLKAAoAlEpWLqK28QCiAW0PBghMKWev3xALzEoQgB3cxE7FQAZQwBnYAAeKNiydwA+YkwYt0CwaL8A4NDLcIAzAHsPE2NgYkM81h4a4jjiRnqaxEQLG3Diax67DKTs6IA6AEE4OBNAkNrEFphPFzcwMzMwnoBfIrsyiqriACs6huPm4SOOrv6+/pVgMABPHZuXu+VaAE4l7zARpxchW6722wLuAGMcMBwQALYgmewAD3BhAADsBDKUoKRru9em93pxPiZCOsCXZQXiON9aOoRgARQi4R4mQSsVhksG2Sk9PaEHCw+EANxwHkwNWxgyy7miuPCt3emAZMVRpWihCB/R5ylBoPExDgTMIAHMoSRacQAGKGMCEMZQOCW8rGwjAMbRR5QcFxAAqAGUyIawIsffYhWQ3WBjdEUiZ/YH3AB+Yjqh3uENhiNjKO5UkifUMBxeFYKHa0CTW232x3O13uz3egG/RZNtxwTNQSPRlLJ1uEOAbZRltQ6C1N8w7BV2L66EYAJWgJnMfjSfbgiZGAEkoELSgBrDXAGExRYzEz0s0+wyOQgjABypSCa053Joeroaika47XdyIAc4adtm0almC5ZFss/Y/sB0Tnpe14kIQxb9hebhyviXIqGuqEkP4SGQXAOGDsQ2rEMOxA4VeN4Qd4hFmr0xAusADS6jQBZSIk0oeP+m70i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