golang support 'Defer Call' and when c++, I use this trick(?).
struct DeferCall
{
DeferCall() {}
~DeferCall() { doSomeThing(); }
}
void SomeMethod()
{
DeferCall deferCall;
...
}
how can I do this in c# ?
I need like this golang defer tutorial
The nearest language equivalent will be try
-finally
:
try
{
DoSomething();
}
finally
{
DoCleanup();
}
The nearest framework equivalent will be IDisposable
+ using
:
using (var stream = File.OpenRead("foo.txt"))
{
// do something
}
Personally, defer
term confuses me, since this is not "deferred execution" (schedule now - execute later, which could be implemented via tasks),
but some sort of RAII implementation.
P.S. Assuming, that you will continue to learn golang:
panic
equivalent is throw
recover
equivalent is catch
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With