Is it possible to create a class, or operator, that would work like using does?
Example:
using (IDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
count++;
}
}
Often we use the using
statement to not have to handle post operations manually, like dispose
and such.
I have in mind a couple of usage of this mechanic.
But I cannot figure out how to achieve this.
An example of actual implementation:
MyClass mc = new MyClass();
MyClass sub = mc.GoDown();
//Do things on sub
sub.GoUp();
What I would like it to be:
MyClass mc = new MyClass();
mc.GoDown {
//Do things on sub
} // GoUp
I know I could use a try {} finally {}
. I am just curious if there is a way to do what using
is doing.
Edit: I do know about the IDispose
implementation. I am just wondering if we can do what using
does without using
.
Edit #2: Improved the example.
Aucun commentaire:
Enregistrer un commentaire