Test if a Property is not Null before Returning

You need a member variable and a full property declaration. Automatically implemented properties are only applicable if they’re trivial wrappers around a field, with no logic involved. You can simplify your getter code slightly, btw:

get
{
    if (_myProperty == null)
    {
       _myProperty = XYZ;
    }
    return _myProperty;
}

(Note that none of this is thread-safe without extra locking, but I assume that’s okay.)

By the way, you already have a private member variable if you’re using automatically implemented properties – it’s just that the compiler’s generating it for you.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)