惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Attack and Defense Labs
Attack and Defense Labs
Engineering at Meta
Engineering at Meta
腾讯CDC
J
Java Code Geeks
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
G
Google Developers Blog
博客园 - 三生石上(FineUI控件)
Microsoft Azure Blog
Microsoft Azure Blog
The Cloudflare Blog
aimingoo的专栏
aimingoo的专栏
T
The Blog of Author Tim Ferriss
D
Darknet – Hacking Tools, Hacker News & Cyber Security
WordPress大学
WordPress大学
博客园_首页
I
InfoQ
NISL@THU
NISL@THU
Recorded Future
Recorded Future
Security Latest
Security Latest
K
Kaspersky official blog
L
Lohrmann on Cybersecurity
T
The Exploit Database - CXSecurity.com
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
U
Unit 42
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
www.infosecurity-magazine.com
www.infosecurity-magazine.com
C
CERT Recently Published Vulnerability Notes
W
WeLiveSecurity
Webroot Blog
Webroot Blog
Last Week in AI
Last Week in AI
AWS News Blog
AWS News Blog
IT之家
IT之家
S
Schneier on Security
雷峰网
雷峰网
Know Your Adversary
Know Your Adversary
T
Threat Research - Cisco Blogs
C
CXSECURITY Database RSS Feed - CXSecurity.com
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
L
LangChain Blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
月光博客
月光博客
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Stack Overflow Blog
Stack Overflow Blog
有赞技术团队
有赞技术团队
云风的 BLOG
云风的 BLOG
G
GRAHAM CLULEY
O
OpenAI News
The Last Watchdog
The Last Watchdog
TaoSecurity Blog
TaoSecurity Blog
The GitHub Blog
The GitHub Blog
V
Vulnerabilities – Threatpost

博客园 - jack_Meng

WinForm 双屏开发中屏幕适配与窗口定位 高考志愿填报之计算机类专业院校报考指南(2026版) WebSocket 快速入门教程(附示例源码) 自动生成项目工具--AutoBuilder,一键干掉80%的重复CRUD工作 C#/.NET/.NET Core优秀项目和框架2026年5月简报 在Win10系统中,默认使用照片查看器 C#实现控制台多区域输出 解决: 您的连接不是私密连接,您目前无法访问 因为此网站使用了 HSTS 小说下载 网页版时间,可全屏显示 个人笔记本连接公共WIFI的安全措施 追更 HelloGitHub 一整年,年度盘点 基于Rust开发的m3u8下载器M3U8Quicker:支持断点续传、边下边播 新写了个直播录制工具,可录制抖音快手斗鱼直播 bing 每日一图 --- 桌面壁纸 一款基于 C# 开发的 Windows 10/11 系统增强优化工具 适合个人的免费域名 语雀里存了三年的笔记,导出到了本地插件----YuqueOut C# 也能像 Python 一样写脚本 | .NET 10 构建基于文件的应用 js 双击页面 开始/暂停 页面滚动 使用bat批量给txt追加内容 Python摄像头监控:运动检测+自动录像+时间水印 【译】告别繁琐查错:认识下新的 Visual Studio Debugger Agent Workflow 好消息,在 Visual Studio 中可以免费使用 GitHub Copilot 了! [C#] 零依赖高性能跨平台 Web 胶水库 -- PicoServer 我的第一款独立产品--TaskManager 写 EF Core 查询,优化查询语句 最新.NET新手入门学习网站合集(2026更新版) Avalonia UI:.NET 跨平台桌面开发的“真香”选择 一个 txt 生成并保存 mp3的 Python 脚本 开源可商用 .NET Office 转 PDF 工具/库 - MiniPdf c# 常用且免费的 AI 编程工具 CodeGeeX,在vs 2019中使用的 AI 大模型工具 支持在 vs studio 2019中安装的 免费的 AI 辅助工具 微软官网wpf例程源码下载地址 GitHub“神级”项目学习网址 开源协议介绍 Windows 窗体控件和等效的 WPF 控件 sql server backup database 爬取七猫中文网小说 ASP.NET Core 内存缓存实战 CliWrap —— .NET 最优雅的命令行交互库 .NET 高级开发 | 设计、实现一个事件总线框架 一个基于 .NET Core + Vue3 构建的开源全栈平台 Admin 系统 C#/.NET/.NET Core优秀项目和框架2026年3月简报--工作流引擎 C# 如何减少代码运行时间:7 个实战优化技巧
.Net Framework 使用Lazy<T>延迟初始化对象
jack_Meng · 2026-04-17 · via 博客园 - jack_Meng

1、延迟初始化定义

一个对象的延迟初始化意味着该对象的创建延迟至第一次使用该对象时

2、延迟初始化目的

主要用于提高性能,避免浪费计算,减少程序内存要求

3、使用形式   Lazy<T>

当使用Lazy<T> 对象的 .Value 属性时,对象才会被创建;如下构建一个Orders类

4、使用示例

测试对象

复制代码

    public class Orders
    {
        public List<string> OrderData { get; }
        public Orders()
        {
            Console.WriteLine("正在加载订单数据...");
            Thread.Sleep(2000); // 模拟耗时
            OrderData = new List<string> { "Order 1", "Order 2", "Order 3" };
            Console.WriteLine("订单数据加载完成。");
        }
    }

复制代码

主程序

复制代码

using LazyDemo;

class Program
{
    private static Lazy<Orders> orders = new Lazy<Orders>();

    static void Main(string[] args)
    {
        Console.WriteLine("程序启动,Orders对象尚未创建");

        DisplayOrders(orders.Value.OrderData);

    }

    static void DisplayOrders(List<string> orders)
    {
        foreach (var o in orders)
        { 
            Console.WriteLine(o);
        }
    }
}

复制代码

运行结果

ctrl+f5运行后

image

 当使用orders.Value时,oders的构造函数才被执行

=======================================================================================