Using (keyword)
From The Oxygene Language Wiki
This is a Language topic
Feel free to add your notes to this topic below.
The using statement provides the combined functionality of "with" and a "try/finally" block to Dispose a resource automatically after using it.
Example
using f := new FileStream(...) do begin ... end;
The above code is equivalent to:
with f := new FileStream(...) do try ... finally if (f is Idisposable) then (f as IDisposable).Dispose(); end;
Important: the object in the "using" statement must be a construct that supports the standard .NET IDisposable interface, if it doesn't, or if the value is nil, it will ignore it and skip the Dispose call.
.NET's Garbage Collector (GC) manages memory usage but does not handle resources such as file handles. In order to free files for others to access, you need to dispose of such objects. See Finalizers for more information.
Remarks
A "using variable", that might potentially hide another local variable will yield a compilation error.
var a := 1; using a := connection.CreateCommand do begin end;
If a happened to be a field of this class, it would only yield a PW12 warning (name clash).
See Also
Area: Oxygene Language
Compiler version: Oxygene 5
Language Glossary — Keywords — Types — FAQ — How To