c# - Lazy load properties with Async -


i've learned lazy load properties in repository. i'd that, need load web page (using httpclient) means property async.

public async task<list<newsmodel>> news {         {         if (_news == null)         {             cacheprovider cache = new cacheprovider();             object cachednews = cache.get("news");              if (cachednews == null)             {                 var client = new httpclient();                 // await httpresponse             }          }         return _news;     }      set     {         _news = value;     } } 

however, visual studio telling me

"the modifier async not valid item"

while highlighting word "news" in first line.

is possible this? or have write separate method?

first of all, properties side-effects not good.

in case, reading property kick off thread, network traffic, , processing on remote server.

this should method, not property.

secondly, compiler right, properties not allowed asynchronous. now, can write property returns asynchronous task, you're not allowed use async keyword. take away async keyword property declaration.

but fact async not legal on properties clue should write method, not property.

note async keyword not required in code you've posted, since you're not using await keyword in there. such, can remove async keyword altogether, since not required. in fact, if change on method, compiler tell unnecessary since you're not using await. (removed after op edited question.)


Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

php - joomla get content in onBeforeCompileHead function -