What is deferred loading?
Lazy loading is the name of the delayed load, which is loaded when it is actually used .
Usually when creating a large object, some properties we can use to create (set the value of the property), which can effectively improve the performance of the system.
Example:
/defines a hero type Public classhero{ Public stringname{Get;Set;} Public stringfullname{Get;Set;} PublicSkill Objskill; PublicHero (stringname) {Name=name; FullName="Super"+name; Objskill=NewSkill (name); }}//define a skill type Public classskill{ Public stringname{Get;Set;} Public intpower{Get;Set;} PublicSkill (stringname) {Name=name; Power=name. Length; }} Public classprogram{ Public Static voidMain (string[] args) {Hero Hero=NewHero ("qi da sheng"); //at this point I just want to get hero's FullName, but also call skill's constructor, load the skill properties,//initializing skill requires a certain amount of space in memory, resulting in unnecessary wasted spaceConsole.WriteLine (Hero. FullName); //think: What if the implementation actually creates the skill object when calling Skill.name? }}
Improve A
Improvement One://defines a hero type Public classhero{ Public stringname{Get;Set;} Public stringfullname{Get;Set;} //Public Skill Objskill; PrivateSkill _skill; PublicSkill Objskill {Get{return_skill?? (New_skill (Name));} } PublicHero (stringname) {Name=name; FullName="Super"+name; //objskill=new Skill (name); }}//define a skill type Public classskill{ Public stringname{Get;Set;} Public intpower{Get;Set;} PublicSkill (stringname) {Name=name; Power=name. Length; }} Public classprogram{ Public Static voidMain (string[] args) {Hero Hero=NewHero ("qi da sheng"); //when you get hero FullName, you do not create an instance of skillConsole.WriteLine (Hero. FullName); //do you really use Skill.name to create an instance of skill, which results in lazy loading? Console.WriteLine (Hero. Objskill.power); //Think 2: Is there any other better way? }}
Improved two
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespacedemo_lazy{//defines a hero type Public classHero { Public stringName {Get;Set; } Public stringFullName {Get;Set; } Private ReadOnlyLazy<skill>skill; PublicSkill Objskill {Get{returnskill. Value; } } PublicHero (stringname) {Name=name; FullName="Super"+name; Skill=NewLazy<skill> (() =NewSkill (Name)); } } //define a skill type Public classSkill { Public stringName {Get;Set; } Public intPower {Get;Set; } PublicSkill (stringname) {Name=name; Power=name. Length; } } Public classProgram { Public Static voidMain (string[] args) {Hero Hero=NewHero ("qi da sheng"); //when you get hero FullName, you do not create an instance of skillConsole.WriteLine (Hero. FullName); //skill instances are created when the skill.name is actually used, resulting in a lazy loading effectConsole.WriteLine (Hero.objSkill.Power); Console.read (); } }}
Advantages of Lazy<t>
So why introduce lazy<t> if we can already implement it using the property caching method?
At least lazy<t> has the following advantages:
- It has lazythreadsafetymode, but we don't normally use it unless it's a critical case (181 words omitted here)
- It makes the definition row of a property simpler
- Semantically, it's more explicit and more readable.
- It allows NULL to be a valid value
Lazy Loading in C #