Skip to content

'await using' errors if IAsyncDisposable is not defined even though it works on types that don't implement IAsyncDisposable #45111

@jnm2

Description

@jnm2

This should not create an error (sharplab):

using System.Threading.Tasks;

class C
{
    static async Task M()
    {
        // ❌ CS0518: Predefined type 'System.IAsyncDisposable' is not defined or imported
        await using (new C()) { }
    }
    
    public Task DisposeAsync() => Task.CompletedTask;
}

The workaround is to declare some unused type named System.IAsyncDisposable. It doesn't have to contain any members and it doesn't even have to be an interface (sharplab):

using System;
using System.Threading.Tasks;

class C
{
    static async Task M()
    {
        await using (new C()) { }
    }
    
    public Task DisposeAsync() => Task.CompletedTask;
}


namespace System
{
    public enum IAsyncDisposable
    {
    }
}

Metadata

Metadata

Assignees

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions