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