2017-02-15 3 views
0

try/finally 블록으로이 구문을 바꾸는 방법은 무엇입니까?동등 try/finally 블록으로 using 문 바꾸기

using(MemoryStream ms = new MemoryStream()){} 

올바른 방법입니까?

MemoryStream ms = new MemoryStream(); 
try 
{ 
    //code 
} 
finally 
{ 
    ms.Dispose();     
} 
+0

왜 그렇게하고 싶습니까?. try catch 내에서 using 블록을 사용하지 않는 이유는 무엇입니까 ?? – NicoRiff

답변

3

그것은 오히려 다음과 같이이다 :

MemoryStream ms = null; 
try 
{ 
    ms = new MemoryStream(); 

    //code 
} 
finally 
{ 
    if (ms != null) ms.Dispose();     
} 

이유는 단순한 인스턴스 일회용 리소스를 생성 할 수 있다는 것이다.