nullable type and a ReSharper warning

Posted by Sarah Vessels on Stack Overflow See other posts from Stack Overflow or by Sarah Vessels
Published on 2010-05-14T16:10:09Z Indexed on 2010/05/14 16:14 UTC
Read the original article Hit count: 234

Filed under:
|
|
|

I have the following code:

private static LogLevel? _logLevel = null;

public static LogLevel LogLevel
{
    get
    {
        if (!_logLevel.HasValue)
        {
            _logLevel = readLogLevelFromFile();
        }

        return _logLevel.Value;
    }
}

private static LogLevel readLogLevelFromFile() { ... }

I get a ReSharper warning on the return statement about a possible System.InvalidOperationException and it suggests I check _logLevel to see if it is null first. However, readLogLevelFromFile returns LogLevel, not LogLevel?, so there is no way the return statement could be reached when _logLevel is null. Is this just an oversight by ReSharper, or am I missing something?

© Stack Overflow or respective owner

Related posts about c#

Related posts about resharper