相关疑难解决方法(0)

尝试/终于在C#中的开销?

我们已经看到很多关于何时以及为何使用try/ catchtry/ catch/的问题finally.我知道try/ 肯定有一个用例finally(特别是因为它是using语句实现的方式).

我们还看到了有关try/catch和异常开销的问题.

然而,我所链接的问题并没有谈到JUST try-finally的开销.

假设try块中发生的任何事情都没有异常,那么确保finally语句在离开try块时执行的开销是多少(有时是从函数返回)?

再一次,我只询问try/ finally,不catch,不抛出异常.

谢谢!

编辑:好的,我将尝试更好地展示我的用例.

我应该使用哪个,DoWithTryFinally或者DoWithoutTryFinally

public bool DoWithTryFinally()
{
  this.IsBusy = true;

  try
  {
    if (DoLongCheckThatWillNotThrowException())
    {
      this.DebugLogSuccess();
      return true;
    }
    else
    {
      this.ErrorLogFailure();
      return false;
    }
  }
  finally
  {
    this.IsBusy = false;
  }
}

public bool DoWithoutTryFinally()
{
  this.IsBusy …
Run Code Online (Sandbox Code Playgroud)

.net c# performance try-finally

71
推荐指数
6
解决办法
5276
查看次数

标签 统计

.net ×1

c# ×1

performance ×1

try-finally ×1