using declaration
A local variable declared with using, as in using var stream = File.OpenRead(path);, whose Dispose method runs automatically at the end of the enclosing scope.
In more detail
Dispose runs even if an exception is unwinding the scope. The older using statement does the same with braces around a block.
Related terms
- IDisposable — The .NET interface with a single Dispose method, implemented by types that own something the garbage collector cannot clean up, such as a file, socket or database connection.
- using directive — A line such as using System.Text; that lets a file refer to types in a namespace by their short names.
Taught in
- Classes, Interfaces, and Object-Oriented C# — Modern .NET Core, C# & Enterprise Microservices
- Code Quality: .editorconfig and Roslyn Analyzers — Modern .NET Core, C# & Enterprise Microservices
- The Dependency Injection Container: Scopes and Lifetimes — Modern .NET Core, C# & Enterprise Microservices