我应该实现IDisposable吗? [英] Should I implement IDisposable?

查看:88
本文介绍了我应该实现IDisposable吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在下面的SQL类中实现IDisposable吗?我已经找到了何时使用IDisposable的文章,但是在进行以下类似操作时没有人解释它.谢谢!

Would I want to implement IDisposable in the SQL class below? I have found articles when to use IDisposable, but none explain it when doing something like below. Thanks!

class main : SQL
{
    void GetTheData()
    {
        DoSQL();
    }
}

class SQL
{
    void DoSQL() 
    {
        using(SQLConnection sq=new SQLConnection()) 
        {
            //SQL Stuff Here
        }    
    }
}

推荐答案

SQLConnection是在方法内部创建和销毁的,所以我拒绝,不要实现IDisposable .如果连接对象是SQL类的成员,那么我的答案将有所不同.
The SQLConnection is created and destroyed inside of a method so I''d say no, don''t implement IDisposable. If the connection object was a member of the SQL class then my answer would be different.


感谢Jimmanuel.我读过的每一篇文章都说,当成员是一次性的时(例如我有一个sqlconnection不在using语句中或通过构造函数传递时)实现了idisposable.

我找不到一篇很好的文章来展示我提供的示例.
Thanks Jimmanuel. Every article I have read says to implement idisposable when the members are disposable (like if I had a sqlconnection not in using statement or passed via constructor).

I couldn''t find a good article showing an example like what I provided.


这篇关于我应该实现IDisposable吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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