.net - C# Property Access Optimization -


in c# (or vb .net), compiler make attempts optimize property accesses? eg.,

  public viewclass view {         {         ...         computed here         ....     } }  if (view != null)     view.something = somethingelse;   

i imagine if compiler somehow detect view remains constant between 2 accesses, can refrain computing value twice. these kind of optimizations performed?

i understand if view has intensive computations, should refactored function (getview()). in particular case, view involves climbing visual tree looking element of particular type.

related: references on workings of (microsoft) c# compiler?

not in general, no. steven mentioned there numerous factors consider regarding multithreading, if computing might change, you're correct should refactored away property. if won't change, should lazy-load (check if private member null, if calculate, return value).

if won't change , depends on parameter, can use dictionary or hashtable cache - given parameter (key) store value. have each entry weakreference value too, when value isn't referenced anywhere , garbage collection happens, memory freed.

hope helps.


Comments

Popular posts from this blog

c++ - How do I get a multi line tooltip in MFC -

asp.net - In javascript how to find the height and width -

c# - DataTable to EnumerableRowCollection -