Zero Coalescing Operator in Access Method

I looked at stackoverflow, not to mention whether there would be null coalescing statements in the access method, any performance implications.

Before:

private Uri _Url;
public Uri Url
{
    if(_Url == null)
        _Url = new Uri(Utilities.GenerateUri());
    return _Url;
}

After:

private Uri _Url;
public Uri Url 
{
    get 
    {
        return _Url = _Url ?? new Uri(Utilities.GenerateUri());
    }
}

I'm not even sure if the syntax is correct, but when I debug, the private object is set.

Before someone asks what is the point of doing this, we discussed within ourselves whether to write for readability (the first seems more readable to me) or write for performance.

I do not know if the compiler will optimize? better than manual zero checking all the time. Micro-optimization is bad, but I'm just curious

-1
1

:

return _Url ?? (_Url = new Uri(Utilities.GenerateUri()));

, , if, .

+2

All Articles