微軟自帶的注釋摘要

// 摘要:
// 定義一種釋放分配的資源的方法。
[ComVisible(true)]
public interface IDisposable
{
// 摘要:
// 執行與釋放或重置非托管資源相關的應用程序定義的任務。
void Dispose();
}
此接口的主要用途是釋放非托管資源。 當不再使用托管對象時,垃圾回收器會自動釋放分配給該對象的內存。 但無法預測進行垃圾回收的時間。 另外,垃圾回收器對窗口句柄或打開的文件和流等非托管資源一無所知。
將此接口的 方法與垃圾回收器一起使用來顯式釋放非托管資源。 當不再需要對象時,對象的使用者可以調用此方法
因為 IDisposableDispose 實現由類型的使用者調用時,實例屬于自己的資源不再需要,您應將包裝在 SafeHandle (建議使用的替代方法) 的托管對象,則應該重寫ObjectFinalize 來釋放非托管資源,忘記在使用者調用 Dispose條件下。
才能直接,使用非托管資源需要實現 IDisposable。 如果應用程序使用對象實現 IDisposable,不提供 IDisposable 實現。 而,那么,當您使用時完成,應調用對象的.Dispose 實現。 根據編程語言中,可以為此使用以下兩種方式之一:
使用一種語言構造 (在 C# 和 Visual Basic 中的 using 語句。
通過切換到實現 .Dispose 的調用在 try/catch 塊。
//使用一種語言構造 (在 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;
}
}
}另外有需要云服務器可以了解下創新互聯scvps.cn,海內外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業上云的綜合解決方案,具有“安全穩定、簡單易用、服務可用性高、性價比高”等特點與優勢,專為企業上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。
當前題目:IDisposable資源釋放接口-創新互聯
分享路徑:http://www.yijiale78.com/article32/ddcgpc.html
成都網站建設公司_創新互聯,為您提供做網站、響應式網站、品牌網站設計、電子商務、ChatGPT、微信公眾號
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯