c# - Using a private variable in a public property getter -


if have public property has getter, correct use private variable assign value return value or should use return rather setting value of userid? in future plan add more functionality use userid field, , methods added class use userid field. there benefit 1 way or other? there other way should done?

    private string userid;     public string id     {                 {             if (system.web.httpcontext.current.request.headers.allkeys.contains("uid"))             {                 userid = system.web.httpcontext.current.request.headers["uid"].tostring();             }             else             {                 userid = "0000";             }             return userid;         }     } 

the way getter coded right not need assignment, because subsequent calls ignore value set previous methods. however, cache result, this:

private string userid; public string id {     {         if (userid == null) {             if (system.web.httpcontext.current.request.headers.allkeys.contains("uid")) {                 userid = system.web.httpcontext.current.request.headers["uid"].tostring();             } else {                 userid = "0000";             }         }         return userid;     } } 

this implementation avoids reading "uid" repeatedly caching result of initial retrieval in private instance variable.


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -