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();
            }
        }
    }
  • 相关阅读:
    liunx下手动安装git及配置
    Liunx系统下删除自带的JDK及安装需要的JDK版本
    Pipeline简单实现的代码
    HttpClient-post请求,含图片
    Java
    函数及BOM
    JS--EcmaScript
    定位
    浮动
    盒子模型
  • 原文地址:https://www.cnblogs.com/muxueyuan/p/7371992.html
Copyright © 2011-2022 走看看