?? 使用實現(xiàn)了dispose模式的類型.txt
字號:
FileStream 實現(xiàn)了Dispose模式
Close方法并不是Dispose正定義的一部分,有此類型提供Close,而有此則沒有。
一般情況大家不需要調(diào)用Disponse或Close方法
但需要顯示清理非托管資源時,應(yīng)調(diào)用Disponse或Close方法.阻止對象的代齡提升,提高應(yīng)用程序的性能。
using System;
using System.IO;
namespace TestDisp
{
class Class1
{
/// <summary>
/// 應(yīng)用程序的主入口點。
/// </summary>
[STAThread]
static void Main(string[] args)
{
Byte[]bytesToWrite=new byte[]{1,2,3,4,5};
FileStream fs=new FileStream("Temp.dat",FileMode.Create);
fs.Write(bytesToWrite,0,bytesToWrite.Length);
//((IDisposable)fs).Dispose();//方法1:顯式釋放非托管資源
fs.Close();//方法2
File.Delete("Temp.dat");
}
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -