728x90
어떤 경우에도 파일을 닫아야 한다면 IDisposable 인터페이스와 using문을 사용하자
class CC :IDisposable
{
private TextWriter writer = null;
public void Create()
{
writer = File.CreateText("sample.txt");
}
public void Close()
{
Dispose();
}
public void Write()
{
throw new ApplicationException("Sample Exception");
}
public void Dispose()
{
if (writer != null)
{
writer.Close();
}
}
}
class Program
{
static void Main(string[] args)
{
try
{
using (var CC = new CC())
{
CC.Create();
CC.Write();
}
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
}
728x90
'c#' 카테고리의 다른 글
[C#] 쿼리가 너무 많을 때 To.Array() (0) | 2023.01.31 |
---|---|
System.Data.MissingPrimaryKeyException (0) | 2023.01.27 |
[C#] 의미없는 구조체 (0) | 2023.01.26 |
[C#] Out of Memory (0) | 2023.01.26 |
[C#] 무명 메서드2 가이드와 장점 (0) | 2023.01.25 |