延迟加载
延迟加载:数据赋予一个对象时不加载数据,当 .tolist 时进行加载数据,节省性能
实例
public class Hero { public string FullName { get; set; } public string Name { get; set; } private readonly Lazy<SpecialSkill> skill; public SpecialSkill Skill { get { return skill.Value; } } public Hero(string name) { Name = name; FullName = "Super " + name; skill = new Lazy<SpecialSkill>(() => new SpecialSkill(name)); } } public class SpecialSkill { public int Power { get; set; } public string SkillName { get; set; } public int StrengthSpent { get; set; } public SpecialSkill(string name) { Console.WriteLine("loading special skill ....."); Power = name.Length; StrengthSpent = name.Length * 3; SkillName = name + " Blazing"; Console.WriteLine(SkillName + ",... this's what makes a legend!"); } }
当不需要加载 specialskill时,就不加载