4 ms·
C# does have scoped destructors, but for GC reasons they use a separate syntax. `IDisposable` and the `using() { }` block do this. The downside is it's entirel
by sitharus 9y ago
C# does have scoped destructors, but for GC reasons they use a separate syntax. `IDisposable` and the `using() { }` block do this.
The downside is it's entirely opt-in, but it's well known and things like IoC containers can handle it.
- maxxxxx 9y agoPersonally I find the "using" syntax ugly especially with many variables and as you mention it relies on the caller to do the right thing. It would be better if the author of the class could control the behavior better.
- Too 9y agoTwo issues with using is that 1. Its restricted to functions, you cant have an IDisposable member without having to implement IDisposable yourself and there manually close the member. Just allowing an annotation on the variable would have made this so much more ergonomic. 2. The lack of ownership semantics when passing objects as function arguments makes it hard to reason about the program. There is no difference between temporary borrowed reference, ownership transfer and shared ownership so you never know if you are the one that should call close or if one of the functions did or will do in the future.