.NET 4.0的延迟初始化

.NET 4.0的延迟初始化,第1张

延迟初始化意味着延迟一个对象,直到首次用它。主要用来提高性能,减少内存需要。

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());

        }

    }
}


 

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/2090727.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-07-22
下一篇 2022-07-22

发表评论

登录后才能评论

评论列表(0条)

保存