关闭使用块中的连接 [英] closing connections within a using block

查看:187
本文介绍了关闭使用块中的连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在读取大量代码时,我发现实现 IDisposable 的给定 Connection 对象是手动在中使用语句关闭。我看到这个,当我看到与MySQL相关的代码。

On reading lots of code, I find that a given Connection object, which implements IDisposable, is is manually closed within a using statement. I see this when I see code related to MySQL.

它不需要显式关闭。为什么开发人员手动关闭它?

It's not needed to be explicitly closed. Why would the developer close it manually?

using(cnn)
{
    //code is here
    cnn.close();
}

这是一个好的/有用的措施吗?

Is this a good/helpful measure?

推荐答案

中使用块显式关闭是重复,误导和冗余对我来说,是一个的东西。

Explicitly closing in a using block is duplication, misleading and redundant so for me, is a bad thing.

这篇关于关闭使用块中的连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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