Delayed initialization delays object initialization until the first time this object is used. Delayed Initialization is a situation that we often encounter when writing a program. For example, it takes a lot of money to create an object, this object may not be used during system operation. In this case, you can use the delayed initialization function to initialize the object when it is used for the first time, delayed initialization can improve program efficiency and reduce program memory usage. In. NET 4.0 before the implementation of delay initialization, We need to write a specific implementation method (about the implementation of delay initialization, you can refer to this aspx "> http://msdn.microsoft.com/en-us/vcsharp/bb870976.aspx. NET 4.0 Lazy <T> is implemented in a similar way ). In. NET 4.0, a generic class System. Lazy <T> is provided to help us implement delayed initialization.
First, let's look at the following code:
View sourceprint? 01 class Program
02 {
03 static void Main (string [] args)
04 {
05 Lazy <Customer> customer = new Lazy <Customer> ();
06 if (! Customer. IsValueCreated)
07 Console. WriteLine ("Customer has not been initialized. ");
08
09 Console. WriteLine ("customer Name: {0}", customer. Value. Name );
10
11 if (customer. IsValueCreated)
12 Console. WriteLine ("Customer has been initialized. ");
13
14 Console. ReadKey ();
15}
16}
17
18 public class Customer
19 {
20 public Customer ()
21 {
22 Name = "James ";
23 Console. WriteLine ("Call the Customer constructor. ");
24}
25
26 public string Name {get; set ;}
27
28 public List <Order> Orders {get; set ;}
29}
30
31 public class Order
32 {
33 public string ID {get; set ;}
34}
The output result is as follows:
Customer has not been initialized.
Call the Customer constructor.
Customer name: James
Customer has been initialized.
The customer is initialized only when the Customer. Value is called. The Value of the read-only attribute returns the delayed initialization object. The Boolean attribute IsValueCreated identifies whether the delayed initialization object has been initialized. In the preceding sample code, the value of the Name attribute is set in the constructor of the Customer class. We can use Lazy <T> overload function Lazy <T> (Func <T>) input a delegate with a returned value to set the attribute value of the delayed initialization object.
View sourceprint? 1 Lazy <Customer> customer1 = new Lazy <Customer> (
2 () =>
3 {
4 return new Customer {Name = ""};
5 });
The output result is as follows:
Customer has not been initialized.
Call the Customer constructor.
Customer name: Li Si
Customer has been initialized.
We can see that the Value attribute of customer1 is initialized by the parameter passed in by the delegate.
To implement delayed initialization of Public attributes, define the fields corresponding to the attributes as Lazy <T>, and then return the Value Attribute of the fields through the get accessors.
View sourceprint? 01 public class Customer
02 {
03 public Customer ()
04 {
05 Name = "James ";
06 Console. WriteLine ("Call the Customer constructor. ");
07
08 _ orders = new Lazy <List <Order> () =>
09 {
10 // Initialization
11 return new List <Order>
12 {
13 new Order {ID = "1 "},
14 new Order {ID = "2 "},
15 new Order {ID = "3 "}
16 };
17}
18 );
19}
20
21 public string Name {get; set ;}
22
23 private Lazy <List <Order> _ orders;
24 public List <Order> Orders
25 {
26 get
27 {
28 return _ orders. Value; // will be created at the first access
29}
30}
31}
32
33 public class Order
34 {
35 public string ID {get; set ;}
36}
Lazy <T> object initialization is thread-safe by default. In a multi-threaded environment, the first thread that accesses the Value Attribute of the Lazy <T> object will initialize the Lazy <T> object, in the future, all accessed threads will use the data initialized for the first time.
Reference: http://msdn.microsoft