LazyT 提供对延迟初始化的支持
生活随笔
收集整理的這篇文章主要介紹了
LazyT 提供对延迟初始化的支持
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
延遲初始化 ?就是在第一次使用的時候在 進行類的初始化
?
public class Student{public Student(){this.Name = "DefaultName";this.Age = 0;Console.WriteLine("Student is init...");}public string Name { get; set; }public int Age { get; set; }} public static void Main(string[] args){Lazy<Student> stu = new Lazy<Student>();if (!stu.IsValueCreated)Console.WriteLine("Student isn't init!");Console.WriteLine(stu.Value.Name);stu.Value.Name = "Tom"; Console.WriteLine(stu.Value.Name);Console.Read();}?
輸出:
Student isn't init!
Student is init...
DefaultName
Tom
他是在輸出Name屬性時才進行初始化的,也就是在第一次使用時才實例化,這樣就可以減少不必要的開銷。
?
轉載于:https://www.cnblogs.com/dragon-L/p/5949725.html
總結
以上是生活随笔為你收集整理的LazyT 提供对延迟初始化的支持的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: js正则归纳
- 下一篇: ctx.close() 和 ctx.ch