Why would try/finally rather than a “using” statement help avoid a race condition?

后端 未结 4 1738
眼角桃花
眼角桃花 2020-11-29 07:29

This question relates to a comment in another posting here: Cancelling an Entity Framework Query

I will reproduce the code example from there for clarity:

         


        
4条回答
  •  难免孤独
    2020-11-29 08:03

    That comment doesn't make any sense as a using statement will be translated to a try/finally block by the compiler. Since 'entities' will not be used outside of the scope, it's easier to use a using statment as this will automatically dispose of the resources.

    You can read more about this on MSDN: using Statement (C# Reference).

提交回复
热议问题