Interface for releasing idisposable Resources

Source: Internet
Author: User

Comments summary provided by Microsoft

// Summary:

// Define a method for releasing allocated resources.

[Comvisible (true)]

Public interface idisposable

{

// Summary:

// Execute the tasks defined by the application related to releasing or resetting unmanaged resources.

Void dispose ();

}


This interface is mainly used to release unmanaged resources. When a hosted object is no longer used, the garbage collector Automatically releases the memory allocated to the object. However, the time for garbage collection cannot be predicted. In addition, the garbage collector knows nothing about window handles, opened files, streams, and other unmanaged resources.

Use the dispose method of this interface with the garbage collector to explicitly release unmanaged resources. When an object is no longer needed, the user of the object can call this method.

Because idisposable. when dispose is called by a user of the type, the instance is no longer needed for its own resources. If you want to wrap the hosted object in safehandle (recommended alternative), you should rewrite the object. finalize is used to release unmanaged resources. If you forget to call dispose.

Implement idisposable using objects

To use unmanaged resources directly, you need to implement idisposable. If an application uses an object to implement idisposable, idisposable is not provided. However, you should call the idisposable. Dispose Implementation of the object when using it. You can use either of the following methods in programming languages:

    • It is constructed in a language (using statements in C # and Visual Basic.

    • The call to implement idisposable. Dispose is implemented in the try/Catch Block.


//使用一种语言构造 (在 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;            }     }}


This article is from the coffee blog, please be sure to keep this source http://4837471.blog.51cto.com/4827471/1561802

Interface for releasing idisposable Resources

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.