延迟初始化出现于.NET 4.0,主要用于提高性能,避免浪费计算,并减少程序内存要求。也可以称为,按需加载。

基本语法:

Lazy<T> xx = new Lazy<T>();//xx代表变量名

举例实现:

首先创建一个Student类,代码如下:

using System;

namespace LazyTest
{
    class Student
    {
        public Student()
        {
            this.Name = "DefaultName";
            Console.WriteLine("调用Student的构造函数");
        }
        public Student(string Name)
        {
            this.Name = Name;
            Console.WriteLine("调用Student(string Name)的构造函数");
        }

        public string Name { get; set; }
    }
}
View Code

相关文章:

  • 2022-12-23
  • 2021-06-01
  • 2021-10-08
  • 2022-02-25
  • 2021-07-25
  • 2021-12-01
猜你喜欢
  • 2021-11-21
  • 2021-11-25
  • 2022-12-23
  • 2021-12-19
  • 2022-12-23
  • 2021-12-28
相关资源
相似解决方案