Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ReSharper suggests "convert to ?? expression", but how?

Using ReSharper 7.1.1 in Visual Studio 2012. Sample code:

private string _str;

private string TheString
{
    get
    {
        if (_str == null) // "X"
        {
            _str = GetString();
        }
        return _str;
    }
}

// do some work to get string. e.g. read from database
private string GetString()
{
    return "blah";
}    

At the line marked "X", ReSharper underlines the "if" statement and suggests "Convert to ?? expression". But how? Am I missing something?

like image 771
Moe Sisko Avatar asked Oct 17 '13 00:10

Moe Sisko


2 Answers

Given your code,

Click on the 'if' where its saying it wants to use the ??

press ALT-ENTER

or click on the lightbulb

it will then have an option to convert, either press enter, or click on it with the mouse and you will get

        private string _str;

        private string TheString
        {
            get { return _str ?? (_str = GetString()); }
        }
like image 142
Keith Nicholas Avatar answered Oct 03 '22 15:10

Keith Nicholas


Keith pretty much answered your question. Here's the screenshot of the light blub:

enter image description here

enter image description here

like image 43
Win Avatar answered Oct 03 '22 16:10

Win