.NET - 不会返回prevent对象处理 [英] .NET - Does Return prevent objects disposing

查看:146
本文介绍了.NET - 不会返回prevent对象处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于以下code:

  Function GetSomething() As Integer
    Using dbConn As New SqlConnection("Connection_String")
      dbConn.Open()
      Using dbCmd As New SqlCommand(" SELECT SOMETHING ....", dbConn)
        Return Integer.Parse(dbCmd.ExecuteScalar())
      End Using
      dbConn.Close()
    End Using
  End Function

请问返回prevent执行,即数据库连接的关闭和隐含的Dispose()在使用块结束时调用的功能块的其余部分?

Will the Return prevent execution of the remainder of the function block i.e. the closing of the database connection and the implied Dispose() called when the Using block finishes?

推荐答案

没有,一个 使用语句相当于一个<一个href="http://msdn.microsoft.com/en-us/library/fk6t46tz.aspx"><$c$c>Try/<$c$c>Finally声明 - 所以处置被执行为执行离开该块,不管是通过到达该块的末端,正常返回,或经由一个异常

No, a Using statement is equivalent to a Try/Finally statement - so the disposal is executed as execution leaves the block, whether that's by reaching the end of the block, returning normally, or via an exception.

您并不需要显式的关闭呼叫不过,因为你处理的连接反正。

You don't need the explicit Close call though, as you're disposing of the connection anyway.

这篇关于.NET - 不会返回prevent对象处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆