【发布时间】:2019-06-13 04:19:37
【问题描述】:
private string GetFileContent(string path)
{
if(File.Exists(HttpContext.Current.Server.MapPath(path)))
{
FileStream fs=null;
try
{
fs = new FileStream(HttpContext.Current.Server.MapPath(path), FileMode.Open, FileAccess.Read);
using (TextReader tr = new StreamReader(fs))
{
fs.Flush();
return tr.ReadToEnd();
}
}
finally
{
fs.Close();
}
}
}
如果将 FileStream fs 分配给 null 代码运行时没有警告,但我不想将 filestream 分配给 null,即在 using 语句中 fs=null。
那么,不赋值空值的正确写法是什么?
【问题讨论】:
-
查看 Jon Skeet 对 this question 的回答中的 cmets。