vendredi 24 février 2017

Load property lazy loading

I have a property which getter should load its value only the first time. The second time it returns the loaded value without loading it again:

    private Object _MemberValue;

    public Object MemberValue
    {
        get
        {
            if(_MemberValue == null)
            {
                _MemberValue= LoadMember();
            }

            return MemberValue;
        }
    }

In VB.NET there is the Static keyword. With it you don't have to declare a class wide member.

Public Property MemberValue as Object
    Get
        Static value as Object = Nothing

        If (value is Nothing) Then
            value = LoadMember()
        End If

        Return value
    End Get
End Property

In C# there isn't such a keyword.

Are there better C# implementations of this Problem or other patterns?

Aucun commentaire:

Enregistrer un commentaire