我已经在the msdn doc中了解了这段代码:
Stream myStream = null;
OpenFileDialog openFileDialog1 = new OpenFileDialog();
[...] // Some init
try
{
if ((myStream = openFileDialog1.OpenFile()) != null)
{
using (myStream)
{
// Insert code to read the stream here.
}
}
}
但Resharper温和地告诉我,检查null是没用的:
我应该信任Resharper还是Microsoft?
答案 0 :(得分:2)
R#是正确的,如果您反编译该类(请参阅下面的代码),它的实现方式永远不会返回null (它总是返回一个流或抛出例外):
public Stream OpenFile()
{
IntSecurity.FileDialogOpenFile.Demand();
string str = this.FileNamesInternal[0];
if (str == null || str.Length == 0)
throw new ArgumentNullException("FileName");
new FileIOPermission(FileIOPermissionAccess.Read, IntSecurity.UnsafeGetFullPath(str)).Assert();
try
{
return (Stream) new FileStream(str, FileMode.Open, FileAccess.Read, FileShare.Read);
}
finally
{
CodeAccessPermission.RevertAssert();
}
}
那说: