Try/Finally block vs calling dispose?

前端 未结 6 1160
栀梦
栀梦 2020-12-15 11:04

Is there any difference between these two code samples and if not, why does using exist?

StreamWriter writer;
try {
    writer = new StreamWrite         


        
6条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-15 11:38

    Is there any difference between these two code samples

    Yes, using checks for null before calling Dispose (i.e. the actual code it is expanded to introduces a null check).

    why does using exist?

    Because the code is more concise. Just a syntactic sugar.

提交回复
热议问题