问题
Are there guarantees in C# that the using statement won`t inherit the try + finally combinations issues?
The question naturally follows the discussion from other here.
According to the documentation:
using (var font1 = new Font("Arial", 10.0f))
{
byte charset = font1.GdiCharSet;
}
The code example earlier expands to the following code at compile time (note the extra curly braces to create the limited scope for the object):
{
var font1 = new Font("Arial", 10.0f);
try
{
byte charset = font1.GdiCharSet;
}
finally
{
if (font1 != null)
((IDisposable)font1).Dispose();
}
}
So, are there any guarantees that the finally for the .Dispose
will be called all the time in C#? In case it is so, does that mean that the using
will actually expand to something different from just the try + catch combination?
UPDATE
In a nutshell what the question is all about. There is a known issue which discourages the use of the try + finally combination. The issue is discussed here. In the question I am asking whether or not the issue applies to the using
, since the using
is the same as the try + catch combination (according to the documentation linked above).
来源:https://stackoverflow.com/questions/60158924/are-there-guarantees-in-c-sharp-that-the-using-statement-wont-inherit-the-try