.NET 4.0的延迟初始化

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

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

        }

    }
}


 

 

posted @ 2013-03-24 11:24  javawebsoa  Views(172)  Comments(0Edit  收藏  举报