延迟初始化意味着延迟一个对象,直到首次用它。主要用来提高性能,减少内存需要。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace FirstConsoleApplication { class Customer { public int Id { get; set; } public string Name { get; set; } public Customer(int Id, string Name) { this.Id = Id; this.Name = Name; } } class Program { static void Main(string[] args) { Lazy<Customer> cust = new Lazy<Customer>(() => new Customer(1, "Amit")); DisplayCustomer(cust.Value); Console.ReadLine(); } public static void DisplayCustomer(Customer c) { Console.WriteLine(c.Id.ToString()); Console.WriteLine(c.Name.ToString()); } } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)