在处理类实例时,我是否需要明确地处理其所有IDisposable成员?

编程入门 行业动态 更新时间:2024-10-22 13:36:40
本文介绍了在处理类实例时,我是否需要明确地处理其所有IDisposable成员?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个具有 SqlConnection 类型属性的类。 SqlConnection 实现 IDisposable 。我有以下问题:

  • 我的类是否也应该实现 IDisposable 属性类型 IDisposable ?
  • 如果是,我是否需要在处理我的实例时明确地配置属性类?例如:

    public class Helper:IDisposable { //假设它是任何其他IDisposable类型。 SqlConnection只是一个例子。 public SqlConnection SqlConnection {get;组; } $ b $ public void Dispose() { if(SqlConnection!= null) { SqlConnection.Dispose();

    注意:我知道在执行 IDisposable 时有一个模式需要遵循,但我的问题与上述情况非常具体。

    解决方案

    它取决于。如果你的类创建了和拥有 IDisposable ,那么它必须处理它(所以,答案是是)。 如果你的类只是使用 IDisposable 它不能处理它(所以第一个答案通常是,否,第二个答案是否)。 助手类

    公共类助手 { //注意,你可以通过这个属性赋值任意的连接 public SqlConnection SqlConnection {get;组; } .... }

    just uses em> SqlConnection (因为它提供了set),就像

    <$ c $ (SqlConnection con = new SqlConnection(...)){ ... // helper只是使用Connection,所以helper一定不能使用SqlConnection 处置它助手助手=新助手(){ SqlConnection = con; }; ... }

    所以它不得处理连接。相反,像这样的类

    public class Helper:IDisposable { private SqlConnection m_SqlConnection; //注意缺少公共set public SqlConnection SqlConnection { get { return m_SqlConnection; } } ... }

    拥有它的 SqlConnection ,所以它负责处理它:

    using(Helper helper = new Helper(...)){ ... //它是拥有SqlConnection的助手 SqlConnection con = helper.SqlConnection; ... }

    I have a class which has a property of the type SqlConnection. SqlConnection implements IDisposable. I have following questions:

  • Should my class also implement IDisposable just because it has property of type IDisposable?
  • If yes, do i need to Dispose the property explicitly when I am disposing instance of my class? E.g.

    public class Helper : IDisposable { // Assume that it's ANY OTHER IDisposable type. SqlConnection is just an example. public SqlConnection SqlConnection { get; set; } public void Dispose() { if (SqlConnection!= null) { SqlConnection.Dispose(); } } }

  • Note : I know that there is a pattern to be followed while implementing IDisposable but my question is very specific to the case mentioned above.

    解决方案

    It depends. If your class creates and owns the IDisposable it must dispose it (so, both answers are "yes"). If your class just uses IDisposable it must not dispose it (so the first answer is, usually, "no" and the second answer is "no").

    In your case, it seems that Helper class

    public class Helper { // Note, that you can assign any arbitrary Connection via this property public SqlConnection SqlConnection { get; set; } .... }

    just uses SqlConnection (because it provides "set") in the way like

    // It's not helper that owns SqlConnection using (SqlConnection con = new SqlConnection(...)) { ... // helper just uses Connection, so helper must not dispose it Helper helper = new Helper() { SqlConnection = con; }; ... }

    so it must not dispose the connection. On the contrary, a class like that

    public class Helper: IDisposable { private SqlConnection m_SqlConnection; // Note the absence of public "set" public SqlConnection SqlConnection { get { return m_SqlConnection; } } ... }

    owns its SqlConnection so it's responsible for disposing it:

    using (Helper helper = new Helper(...)) { ... // it's helper that owns SqlConnection SqlConnection con = helper.SqlConnection; ... }

    更多推荐

    在处理类实例时,我是否需要明确地处理其所有IDisposable成员?

    本文发布于:2023-11-11 07:46:07,感谢您对本站的认可!
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:实例   成员   IDisposable

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!