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