zoukankan      html  css  js  c++  java
  • Is it a good approach to call return inside using {} statement?

    Is it a good approach to call return inside using {} statement?

    It's perfectly safe to call return inside your using block, since a using block is just a try/finally block.

    In your example above after return true, the scope will get disposed and the value returned. return false, and scope.Complete() will not get called. Dispose however will be called regardless since it reside inside the finally block.

    Your code is essentially the same as this (if that makes it easier to understand):

    var scope = new TransactionScope())
    try
    {
      // my core logic
      return true; // if condition met else
      return false;
      scope.Complete();
    }
    finally
    {
      if( scope != null) 
        ((IDisposable)scope).Dispose();
    }
    

    Please be aware that your transaction will never commit as there's no way to get to scope.Complete() to commit the transaction.

    You should make it clear that Dispose will get called. If the OP doesn’t know what happens in using, chances are he doesn’t know what happens with finally. – Konrad Rudolph Aug 2 '12 at 16:03

    What happens if i return before the end of using statement? Will the dispose be called?

    Yes, Dispose will be called. It's called as soon as the execution leaves the scope of the using block, regardless of what means it took to leave the block, be it the end of execution of the block, a return statement, or an exception.

    As @Noldorin correctly points out, using a using block in code gets compiled into try/finally, with Dispose being called in the finally block. For example the following code:

    using(MemoryStream ms = new MemoryStream())
    {
         //code
         return 0;
    }
    

    effectively becomes:

    MemoryStream ms = new MemoryStream();
    try
    {
        // code
        return 0;
    }
    finally
    {
        ms.Dispose();
    }
    

    So, because finally is guaranteed to execute after the try block has finished execution, regardless of its execution path, Dispose is guaranteed to be called, no matter what.

    For more information, see this MSDN article.

    Addendum:
    Just a little caveat to add: because Dispose is guaranteed to be called, it's almost always a good idea to ensure that Dispose never throws an exception when you implement IDisposable. Unfortunately, there are some classes in the core library that do throw in certain circumstances when Dispose is called -- I'm looking at you, WCF Service Reference / Client Proxy! -- and when that happens it can be very difficult to track down the original exception if Dispose was called during an exception stack unwind, since the original exception gets swallowed in favor of the new exception generated by the Dispose call. It can be maddeningly frustrating. Or is that frustratingly maddening? One of the two. Maybe both.

  • 相关阅读:
    Objective-C 和 Core Foundation 对象相互转换
    sql学习笔记(18)-----------数据库创建过程
    JVM虚拟机结构
    自己定义控件-MultipleTextView(自己主动换行、自己主动补齐宽度的排列多个TextView)
    Blue Jeans
    怎样注冊 diskgroup 到集群
    改动购物项图书数量的Ajax处理
    leetcode_Integer to Roman
    Buildroot 龙芯1C支持指南
    Buildroot構建指南--Overview【转】
  • 原文地址:https://www.cnblogs.com/chucklu/p/15204951.html
Copyright © 2011-2022 走看看