zoukankan      html  css  js  c++  java
  • 延迟加载

    我们创建某一个对象需要很大的消耗,而这个对象在运行过程中又不一定用到,为了避免每次运行都创建该对象,这时候延迟初始化(也叫延迟实例化)就出场了。

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

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

    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; }
        }
    }
    

      

    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();
            }
        }
    }
  • 相关阅读:
    python 图片验证码降噪
    python 视频配音、剪辑
    Python实现音乐的剪辑
    OpenCV-Python 视频读取
    OpenCV-Python 人脸眼睛嘴识别
    OpenCV-Python 人脸识别
    numpy
    python学习笔记(3)---cookie & session
    python爬虫学习笔记(2)-----代理模式
    python爬虫学习笔记(1)
  • 原文地址:https://www.cnblogs.com/muxueyuan/p/7371992.html
Copyright © 2011-2022 走看看