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