© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
64 replies
SWEETPONY

✅ is it a good disposable class?

public class AsyncDisposable                                      
    : Disposable,                                                 
    IAsyncDisposable                                              
{                                                                 
    public bool IsDisposedAsync { get; private set; }             
                                                                  
    public async ValueTask DisposeAsync()                         
    {                                                             
        if ( IsDisposedAsync || IsDisposed )                      
            return;                                               
                                                                  
        _disposingСancellationTokenSource.Cancel();               
                                                                  
        await OnDisposeAsync.InvokeAsync();                       
                                                                  
        await DisposeAsyncCore()                                   
            .ConfigureAwait( continueOnCapturedContext: false );   
                                                                  
        IsDisposedAsync = true;                                   
                                                                  
        Dispose();                                                
        GC.SuppressFinalize( this );                              
    }                                                             
                                                                  
    protected virtual ValueTask DisposeAsyncCore() =>             
        default;                                                  
                                                                  
    public Func<ValueTask> OnDisposeAsync { get; set; }           
}                     
public class AsyncDisposable                                      
    : Disposable,                                                 
    IAsyncDisposable                                              
{                                                                 
    public bool IsDisposedAsync { get; private set; }             
                                                                  
    public async ValueTask DisposeAsync()                         
    {                                                             
        if ( IsDisposedAsync || IsDisposed )                      
            return;                                               
                                                                  
        _disposingСancellationTokenSource.Cancel();               
                                                                  
        await OnDisposeAsync.InvokeAsync();                       
                                                                  
        await DisposeAsyncCore()                                   
            .ConfigureAwait( continueOnCapturedContext: false );   
                                                                  
        IsDisposedAsync = true;                                   
                                                                  
        Dispose();                                                
        GC.SuppressFinalize( this );                              
    }                                                             
                                                                  
    protected virtual ValueTask DisposeAsyncCore() =>             
        default;                                                  
                                                                  
    public Func<ValueTask> OnDisposeAsync { get; set; }           
}                     


what can I change?
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,828Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements
Next page

Similar Threads

✅ Disposable class level property
C#CC# / help
14mo ago
ValueType disposable scope
C#CC# / help
5mo ago
❔ Maybe Disposable Object
C#CC# / help
3y ago
✅ Is it a good idea to store CSS class as a property in my C# model?
C#CC# / help
2y ago