![]() ![]() You can achieve the same result by putting the object inside a try block and then calling Dispose (or DisposeAsync) in a finally block in fact, this is how the using statement and the using declaration are translated by the compiler. For more information on IAsyncDisposable, see Implement a DisposeAsync method.īoth using forms ensure that Dispose (or DisposeAsync) is called even if an exception occurs within the using block. If the object implements IAsyncDisposable instead of IDisposable, either using form calls the DisposeAsync and awaits the returned ValueTask. ![]() A variable declared with a using declaration is read-only. Within the using block, the object is read-only and can't be modified or reassigned. The using statement causes the object itself to go out of scope as soon as Dispose is called. The using declaration calls the Dispose method on the object in the correct way when it goes out of scope. When the lifetime of an IDisposable object is limited to a single method, you should declare and instantiate it in the using statement or using declaration. All such types must implement the IDisposable interface, or the IAsyncDisposable interface. There are many other kinds of unmanaged resources and class library types that encapsulate them. Using var reader = new StringReader(manyLines) įile and Font are examples of managed types that access unmanaged resources (in this case file handles and device contexts). The using declaration doesn't require braces: string manyLines = is line one ![]() Using (var reader = new StringReader(manyLines)) The following example shows how to use the using statement. The language supports asynchronous disposable types that implement the System.IAsyncDisposable interface. The await using statement ensures the correct use of IAsyncDisposable objects. The using statement provides a convenient syntax that ensures the correct use of IDisposable objects. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |