"is c#'s using statement abort-safe?" Code Answer

5

the book's companion web site has more info on aborting threads here.

in short, the first translation is correct (you can tell by looking at the il).

the answer to your second question is that there may be scenarios where the variable can be legitimately null. for instance, getfoo() may return null here, in which you wouldn't want a nullreferenceexception thrown in the implicit finally block:

using (var x = getfoo())
{
   ...
}

to answer your third question, the only way to make abort safe (if you're calling framework code) is to tear down the appdomain afterward. this is actually a practical solution in many cases (it's exactly what linqpad does whenever you cancel a running query).

By Layne Bernardo on April 18 2022

Answers related to “is c#'s using statement abort-safe?”

Only authorized users can answer the Search term. Please sign in first, or register a free account.