Check equality of OpenFileDialog.OpenFile

I read this code in the msdn file :

Stream myStream = null;
OpenFileDialog openFileDialog1 = new OpenFileDialog();
[...] // Some init
try
{
    if ((myStream = openFileDialog1.OpenFile()) != null)
    {
        using (myStream)
        {
            // Insert code to read the stream here.
        }
    }
}

But Resharper gently tell me that checking for null is useless:

enter image description here

Should I trust Resharper or Microsoft?

+4
source share
1 answer

R # is right on this if you decompile the class (see code below), it is implemented in a way that can never return null (it always returns a stream or throws an exception):

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();
    }
}

That said:

+2

All Articles