Language Constructs
To make the dispose pattern less verbose, several languages have some kind of built-in support for it:
The C# language features the using
statement that automatically calls the Dispose
method on an object that implements the IDisposable
interface:
which is equal to:
Resource resource = GetResource try { // Perform actions with the resource. ... } finally { resource.Finalize; }Similarly, the Python language has a with
statement can be used to similar effect:
The Java language introduced a new syntax called try
-with-resources in Java version 7. It can be used on objects that implement the AutoCloseable interface (that defines method close):
Read more about this topic: Dispose Pattern
Famous quotes containing the words language and/or constructs:
“There is ... in every child a painstaking teacher, so skilful that he obtains identical results in all children in all parts of the world. The only language men ever speak perfectly is the one they learn in babyhood, when no one can teach them anything!”
—Maria Montessori (18701952)
“Psychologists have set about describing the true nature of women with a certainty and a sense of their own infallibility rarely found in the secular world.”
—Naomi Weisstein, U.S. psychologist, feminist, and author. Psychology Constructs the Female (1969)