Are there guarantees in C# that the using statement won`t inherit the try + finally combinations issues? [duplicate]

浪尽此生 提交于 2020-03-21 07:17:49

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!