业务场景:
在项目开发中,经常会遇到特定的对象使用的加载问题,有的实例对象我们创建之后并非需要使用,只是根据业务场景来调用,所以可能会导致很多无效的实例加载
延迟初始化出现于.NET 4.0,主要用于提高性能,避免浪费计算,并减少程序内存要求。也可以称为,按需加载
代码事例:
1.未进行延迟加载的情况
a.创建学生类:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Demo_Lazy_ { /// <summary> /// 学生类 /// </summary> public class Student { public Student() { this.Age = 12; Console.WriteLine("调用构造函数"); } /// <summary> /// 年龄 /// </summary> public int Age { get; set; } } }