IDisposable资源释放接口

发布时间:2020-07-20 05:44:24 作者:刺激乐天派
来源:网络 阅读:347

微软自带的注释摘要

// 摘要: 

    //     定义一种释放分配的资源的方法。

    [ComVisible(true)]

    public interface IDisposable

    {

        // 摘要: 

        //     执行与释放或重置非托管资源相关的应用程序定义的任务。

        void Dispose();

    }


此接口的主要用途是释放非托管资源。 当不再使用托管对象时,垃圾回收器会自动释放分配给该对象的内存。 但无法预测进行垃圾回收的时间。 另外,垃圾回收器对窗口句柄或打开的文件和流等非托管资源一无所知。

将此接口的 Dispose 方法与垃圾回收器一起使用来显式释放非托管资源。 当不再需要对象时,对象的使用者可以调用此方法

因为 IDisposable.Dispose 实现由类型的使用者调用时,实例属于自己的资源不再需要,您应将包装在 SafeHandle (建议使用的替代方法) 的托管对象,则应该重写Object.Finalize 来释放非托管资源,忘记在使用者调用 Dispose条件下。

使用对象实现 IDisposable

才能直接,使用非托管资源需要实现 IDisposable 如果应用程序使用对象实现 IDisposable,不提供 IDisposable 实现。 而,那么,当您使用时完成,应调用对象的IDisposable.Dispose 实现。 根据编程语言中,可以为此使用以下两种方式之一:


//使用一种语言构造 (在 C# 和 Visual Basic 中的 using 语句
using System;using System.IO;using System.Text.RegularExpressions;public class WordCount
{
   private String filename = String.Empty;
   private int nWords = 0;
   private String pattern = @"\b\w+\b"; 

   public WordCount(string filename)
   { 
        if (! File.Exists(filename))
                 throw new FileNotFoundException("The file does not exist.");
        this.filename = filename;
        string txt = String.Empty;
        using (StreamReader sr = new StreamReader(filename))
        {
             txt = sr.ReadToEnd();
             sr.Close();
         }
         nWords = Regex.Matches(txt, pattern).Count;
   } 
   public string FullName
   {
        get {
         return filename; 
         } 
    }
    public string Name
   {
         get { 
             return Path.GetFileName(filename); 
         } 
    }
    public int Count 
   { 
           get { 
               return nWords; 
           } 
   }
}
using System;
using System.IO;
using System.Text.RegularExpressions;
public class WordCount
{   
private String filename = String.Empty;   
private int nWords = 0;   
private String pattern = @"\b\w+\b"; 

   public WordCount(string filename)
   { 
        if (! File.Exists(filename))
                 throw new FileNotFoundException("The file does not exist.");      
       this.filename = filename;      
       string txt = String.Empty;
       StreamReader sr = null;
       try {
         sr = new StreamReader(filename);
         txt = sr.ReadToEnd();
         sr.Close();
      }catch {
      
      }
      finally {
               if (sr != null) sr.Dispose();     
      }
      nWords = Regex.Matches(txt, pattern).Count;
   } 
   public string FullName
   { 
       get {
        return filename; 
        } 
    }
    public string Name
   { 
       get { 
           return Path.GetFileName(filename); 
           } 
   }
   public int Count 
   { 
       get { 
           return nWords; 
           } 
    }
}


推荐阅读:
  1. php调用接口及编写接口
  2. python调用各种接口,webservice,c接口,com接口,socket协议方法

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

idisposable接口 资源释放 %d

上一篇:解析json数据

下一篇:Html5,Css初学总结

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》