c# - Initializing member in Getter or Constructor -
is there difference between these 2 approaches ?
in approach using getter initialize datatable
public datatable res { { if (res == null) { res = getdatatable(); } return res ; } private set; }
vs.
in approach using constructor initialize datatable
public class xyz { public datatable res{ get; private set;} //constructor public xyz() { res= getdatatable(); } }
this variable used on asp.net page fill dropdown list. perform better ?
edit:-
this used in web application data not change. bind table dropdown list in page_load event.
the question whether given instance of class xyz
need , use datatable
.
if not, you'd want lazy-initialize (using getter), avoid doing work front nothing.
if need it, next question whether there potentially substantive delay between instantiation of class , call res
property. if so, loading data upon instantiation mean data bit more stale if waited until property getter called.
the flip-side not applicable in web scenario, in other applications 1 want consider whether call property needs highly responsive keep ui freezing. preloading data might preferable in such scenario.
Comments
Post a Comment