1. 概述
我们创建某一个对象需要很大的消耗,而这个对象在运行过程中又不一定用到,为了避免每次运行都创建该对象,这时候延迟初始化(也叫延迟实例化)就出场了。延迟初始化出现于.NET 4.0,主要用于提高性能,避免浪费计算,并减少程序内存要求。也可以称为,按需加载。
2. 基本语法
Lazy<T> xx = new Lazy<T>();//xx代表变量名
3. 举例实现
首先创建一个Student类,代码如下:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
代码using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LazyTest
{
class Student
{
public Student()
{
this.Name = "DefaultName";
Console.WriteLine("调用Student的构造函数");
}
public string Name { get; set; }
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
代码using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LazyTest
{
class Program
{
static void Main(string[] args)
{
Lazy<Student> student = new Lazy<Student>();
if (!student.IsValueCreated)
{
Console.WriteLine("Student未初始化");
}
Console.WriteLine(student.Value.Name);
if (student.IsValueCreated)
{
Console.WriteLine("Student已经初始化");
}
Console.ReadKey();
}
}
}
设置断点调试后发现,在new完之后,student的IsValueCreated的值是false,value的值是null
![image image](https://img2018.cnblogs.com/blog/329170/201912/329170-20191214091904922-1408323785.png)
接着往下走,调用到Name属性时,student的IsValueCreated的值是true,value的值已经不为null了
![image image](https://img2018.cnblogs.com/blog/329170/201912/329170-20191214091905365-892266791.png)
![image image](https://img2018.cnblogs.com/blog/329170/201912/329170-20191214091905639-1132027318.png)
结果可以看出,Student是在输出Name属性才进行初始化的,也就是在第一次使用时才会去初始化,这样就可以达到减少消耗的目的。
这个例子很简单,也是Lazy<T>最基本的使用方式。我们还可以使用 Lazy<T> 的重载函数 Lazy<T> (Func<T>) 传入一个带返回值的委托来设置延迟初始化对象的属性值。
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
代码using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LazyTest
{
class Program
{
static void Main(string[] args)
{
Lazy<Student> student = new Lazy<Student>(() => new Student { Name = "SetName" });
if (!student.IsValueCreated)
{
Console.WriteLine("Student未初始化");
}
Console.WriteLine(student.Value.Name);
if (student.IsValueCreated)
{
Console.WriteLine("Student已经初始化");
}
Console.ReadKey();
}
}
}
![image image](https://img2018.cnblogs.com/blog/329170/201912/329170-20191214091906088-2020894288.png)
注:Lazy<T> 对象初始化默认是线程安全的,在多线程环境下,第一个访问 Lazy<T> 对象的 Value 属性的线程将初始化 Lazy<T> 对象,以后访问的线程都将使用第一次初始化的数据。
4. 应用场景
有一个对象的创建开销很大,而程序可能不会使用它。例如,假定您的程序在启动时加载若干个对象实例,但只有一些对象实例需要立即执行。通过将不必要的对象的初始化延迟到已创建必要的对象之后,可以提高程序的启动性能。
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步