Yea but that doesn’t give you the guarenteed cleanup that the
yield syntax allows. In c++ this would be accomplished by using
a guarded object that would destruct when the scope was exited,
there is no comparable operation in java, without explictely
declaring a wrapping try / catch / finally. In C# V2.0 it is
possible because of the existance of yield. It is not possible
in C# 1.0.
This can also be achieved in C# 1.0 with the ‘using’ statement:
class ExternalResource : System.IDisposable {
public ExternalResource() {
… acquire the resource …
}
public void Dispose() {
… release the resource …
}
}
then code to use it looks like,
...
using ( resource = new ExternalResource() ) {
... use the resource ...
}
...
If the object will be used without a ‘using’ statement then a bit more
logic is required to add a finalizer and co-ordinate between the Dispose
method and the finalizer.
Mike.
···
From: Charles Comstock [mailto:cc1@cec.wustl.edu]