Olipro
IDisposable ownership semantics
This is primarily a question regarding whether or not C# has introduced any sort of syntactic sugar or support for a "nicer" way of expressing transfer of ownership.
For the purposes of this question, nullable types is fully enabled and static analysis that will issue
CA2000
as an error is also turned on.
Consider a method that returns a type that implements IDisposable
and needs to either return it, or ensure it gets Dispose
'd if an exception occurs - Currently I'd write something like this:
Is there anything better than this? This sort of pattern feels about as manual as malloc
and free
in C.28 replies
Selecting and filtering from a list with full nullable safety & warnings as errors
I have the type
Btn
in my sample below which simulates the portion of WinForms that I'm interested in.
I have a list of Btn
objects and my goal is to mutate it into a subset of Btn
objects that have their Tag
matching the Wanted
type without being opaque to the static analyser that detects potential null references (and avoiding the null-forgiving operator)
I ran the code below over on https://sharplab.io and it does not complain that OfType
could return any nullables.
Nonetheless, the code fails because the middle element (which will be a tuple of null
s) still gets returned.
Is this an issue with the REPL or is the static analyser really not seeing that the tuple types could end up being null? Perhaps there is a cleaner way to achieve what I want. Do note that I cannot alter the type held by Btn.Tag
, it's an object in WinForms and I have no control over the code that decided to expose the types via that field.
54 replies