Replies: 12 comments
-
Wouldn't be fixing the docs much simpler thing? ;) |
Beta Was this translation helpful? Give feedback.
-
@ig-sinicyn I have a feeling this might actually lead to much greater confusion since the |
Beta Was this translation helpful? Give feedback.
-
@maelgrove I think that in this case, changing what "dispose" means is better than introducing a completely new concept, interface and possibly syntax. (Just like for example Additionaly, I don't see how "destroy" is any better than "dispose". |
Beta Was this translation helpful? Give feedback.
-
Yes, the MSDN docs are out of date. |
Beta Was this translation helpful? Give feedback.
-
So far, public interface ICriticalDisposable : IDisposable
{
void Dipose(bool isDisposing);
override void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
// allow finalizer, disallow implementation by structs
~ICriticalDisposable() => Dispose(false);
} |
Beta Was this translation helpful? Give feedback.
-
That's kinda what's out of date. You shouldn't be doing any of this for unmanaged resources anymore: void Dispose(bool isDisposing);
override void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
// allow finalizer, disallow implementation by structs
~ICriticalDisposable() => Dispose(false); Instead, wrap it in a SafeHandle (which implements class ClassWithUnmanagedThings : IDisposable
{
public void Dispose()
{
handleOfUnmanagedThing.Dispose();
}
} |
Beta Was this translation helpful? Give feedback.
-
In almost every project I've been involved in¹, I've been highly successful in eliminating the confusion surrounding If the type is defined in the code of the project, then one of the following holds:
Under these rules, ¹ The Roslyn project breaks this in a number of places, but I'm pushing hard to eliminate them over time in practice and then in policy. |
Beta Was this translation helpful? Give feedback.
-
Yes, thank you! Well put. I'm going to link people to this because this is exactly what I have been doing in practice. |
Beta Was this translation helpful? Give feedback.
-
It's so interesting to hear that when other members of the team argue against the use of |
Beta Was this translation helpful? Give feedback.
-
@HaloFour I've heard a few community members argue that but so far no team members. http://joeduffyblog.com/2005/12/27/never-write-a-finalizer-again-well-almost-never/ And an article by @stephentoub so old that you need https://web.archive.org/web/20080704160625/https://msdn.microsoft.com/msdnmag/issues/05/10/Reliability/ (or download the now-archived MSDN mag as .chm) |
Beta Was this translation helpful? Give feedback.
-
Eric Lippert has always defended on StackOverflow a strong stance against usage of I'm personally all-in to allow |
Beta Was this translation helpful? Give feedback.
-
Amusingly, another answer on that very same SO question mentions another dev from the Language Design Team answering a question about
So yeah, the views seem to be fragmented even among the people who make the language. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
A lot of types within the .NET framework, other Microsoft platforms and frameworks (ASP.NET., ASP.NET Core, EF..) as well as 3rd party libraries and tools currently use the
IDisposable
contract to indicate types (mostly infrastructure-related services) which can be cleaned up after use, mostly for stylistic purposes thanks to theusing
statement in C#. However, the MSDN documentation clearly states that...The idea: Provide an
IDestroyable
contract which can be used together withusing
(or a new statement block such asuse
orscope
which indicates that a type will be destroyed using a custom routine when it leaves a certain scope, irregardless of having managed/unmanaged resources.Usage:
Additionally, a
UnmanagedResourcesAttribute
could be introduced that could be used to decorate types implementingIDisposable
which actually contain unmanaged resources. When using a type withIDisposable
which isn't annotated withUnmanagedResourceAttribute
, the compiler could produce a warning indicating thatIDisposable
might have been misused. When a developer allocates a type that implementsIDisposable
and has aUnmanagedResourcesAttribute
, but doesn't dispose it anywhere in code, a warning could be produced that unmanaged resource aren't released during the lifetime of a type (though this could be harder to track).This ultimately would allow continue using the
using
statement for style preferences, but would do away with the confusion whether a type now contains unmanaged resources or not.Edit: I just saw this might be overlapping with #121 and dotnet/roslyn#161, but proposes a slightly different approach by explicit contract implementation.
Beta Was this translation helpful? Give feedback.
All reactions