业务场景:

在项目开发中,经常会遇到特定的对象使用的加载问题,有的实例对象我们创建之后并非需要使用,只是根据业务场景来调用,所以可能会导致很多无效的实例加载

延迟初始化出现于.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; }
    }
}
View Code

相关文章:

  • 2021-05-11
  • 2021-05-22
  • 2022-12-23
  • 2021-11-28
  • 2021-11-22
  • 2021-08-19
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-11-28
  • 2022-02-01
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案