Lazy Loading in C #

Source: Internet
Author: User

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:

    1. It has lazythreadsafetymode, but we don't normally use it unless it's a critical case (181 words omitted here)
    2. It makes the definition row of a property simpler
    3. Semantically, it's more explicit and more readable.
    4. It allows NULL to be a valid value

Lazy Loading in C #

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.