这里已经确定空的使用块不是一种适当的覆盖方式Dispose(),但是下面的情况呢?
这是空using块的合法用途吗?
try
{
using (File.OpenRead(sourceFile)) { }
}
catch (FileNotFoundException)
{
error = "File not found: " + sourceFile;
}
catch (UnauthorizedAccessException)
{
error = "Not authorized to access file: " + sourceFile;
}
catch (Exception e)
{
error = "Error while attempting to read file: " + sourceFile + ".\n\n" + e.Message;
}
if (error != null)
return error;
System.Diagnostics.Process.Start(sourceFile);
Run Code Online (Sandbox Code Playgroud)
Tig*_*ran -1
使用空using块没有多大意义,因为您只需在链的末尾添加finallycatch并Dispose在那里处理:
FileStream fs;
try {
fs = File.OpenRead(sourceFile);
}
catch(..) {
}
catch(..) {
}
catch(..) {
}
finally {
if(fs !=null) {
fs.Close();
fs.Dispose();
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
985 次 |
| 最近记录: |