Skip to content

Commit 114272e

Browse files
committed
describe async using expansion
Include the rule that the result of `DisposeAsync` must be `await`ed.
1 parent 4e871ec commit 114272e

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

standard/statements.md

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1950,7 +1950,7 @@ resource_acquisition
19501950
;
19511951
```
19521952
1953-
A ***resource*** is either a class or non-ref struct that implements either or both of the `System.IDisposable` or `System.IAsyncDisposable` interfaces, which includes a single parameterless method named `Dispose` and/or `DisposeAsync`; or a ref struct that includes a method named `Dispose` having the same signature as that declared by `System.IDisposable`. Code that is using a resource can call `Dispose` or `DisposeAsync` to indicate that the resource is no longer needed.
1953+
A ***resource*** is either a class or non-ref struct that implements either or both of the `System.IDisposable` or `System.`IAsync`Disposable` interfaces, which includes a single parameterless method named `Dispose` and/or `DisposeAsync`; or a ref struct that includes a method named `Dispose` having the same signature as that declared by `System.IDisposable`. Code that is using a resource can call `Dispose` or `DisposeAsync` to indicate that the resource is no longer needed.
19541954
19551955
If the form of *resource_acquisition* is *local_variable_declaration* then the type of the *local_variable_declaration* shall be either `dynamic` or a resource type. If the form of *resource_acquisition* is *expression* then this expression shall have a resource type. If `await` is present, the resource type shall implement `System.IAsyncDisposable`.
19561956
@@ -2101,7 +2101,33 @@ A `using` statement of the form
21012101
await using (ResourceType resource = «expression» ) «statement»
21022102
```
21032103
2104-
corresponds to the expansions shown above with `IAsyncDisposable` instead of `IDisposable`, and `DisposeAsync` instead of `Dispose`.
2104+
corresponds to the expansions shown above with `IAsyncDisposable` instead of `IDisposable`, `DisposeAsync` instead of `Dispose`, and the `Task` returned from `DisposeAsync` is `await`ed. An `await using` of the form:
2105+
2106+
```csharp
2107+
await using (ResourceType resource = «expression» ) «statement»
2108+
```
2109+
2110+
corresponds to
2111+
2112+
```csharp
2113+
{
2114+
ResourceType resource = «expression»;
2115+
try
2116+
{
2117+
«statement»;
2118+
}
2119+
finally
2120+
{
2121+
IAsyncDisposable d = (IAsyncDisposable)resource;
2122+
if (d != null)
2123+
{
2124+
await d.DisposeAsync();
2125+
}
2126+
}
2127+
}
2128+
```
2129+
2130+
When `ResourceType` is a reference type that implements `IAsyncDisposable`. Other expansions for `await using` perform similar substitutions from the synchronous `Dispose` method to the asynchronous `DisposeAsync` method.
21052131

21062132
## 13.15 The yield statement
21072133

0 commit comments

Comments
 (0)