For some "big object" creation, we often want to delay loading, that is, when necessary to create an object instance. Now lazy<t> is well supported by this feature. Mainly include:
- No lazy<t> before
- Lazy<t> instances
- The nature of lazy loading
No lazy<t> before
Before lazy<t>, we implemented lazy loading in the following way.
Public class Lazysinleton
{
Private Lazysingleton ()
{}
Public Static Lazysingleton Instance
{
Get
{
return Lazy.data;
}
}
Private class Lazy
{
Static Lazy ()
{}
Internal Static ReadOnly New Lazysingleton ();
}
}
Above
The way that the Lazysingleton class was created through a constructor is masked by a private constructor
Private nested class The data field of lazy is responsible for providing an instance of Lazysingleton
You can only get the instance represented by the inner class Lazy.data by Lazysingleton property instance.
Lazy<t> instances
First look at the definition of lazy<t>:
Public classLazy<t>{ PublicLazy (); PublicLazy (BOOLIsThreadSafe); PublicLazy (func<t>valuefactory); PublicLazy (lazythreadsafemode mode); PublicLazy (func<t> valuefactory,BOOLIsThreadSafe); PublicLazy (funct<t>valuefactory, LazyThreadSafetyMode mode); Public BOOLisvaluecreated{Get;} PublicT Value {Get;} Public Override stringtostirng ();}
The object is created through the Lazy<t> constructor overload, and the object is created by the Value property that reflects the lazy load, and gets the object instance.
Public class someclass{ publicint id{get; Set;}} LazyNew lazy<someclass>(); Console.WriteLine (temp. value.id);
Above, only if there is no constructor function, if there is a constructor how to deal with it?
--Use public Lazy (func<t> valuefactory) to create an object from a delegate
class someclass{ publicint id{get; Set;} Public SomeClass (int ID) { this. ID= id; }} LazyNewnew Big (+)); Console.WriteLine (temp. value.id);
The nature of lazy loading
Create a custom lazy load class.
Public classMylazy<t> { Private volatile ObjectBoxed//volatile description in multi-threaded condition, you can also modify the field PrivateFunc<t> valuefactory;//a delegate used to produce an instance of a T object StaticMylazy () {} PublicMylazy () {} PublicMylazy (func<t>valuefactory) { This. valuefactory =valuefactory; } PublicT Value {Get{Boxed Boxed=NULL; if( This. boxed! =NULL) {Boxed= This. Boxed asBoxed; if(Boxed! =NULL) { returnBoxed.value; } } return This. Init (); } } //initializing an object instance PrivateT Init () {Boxed Boxed=NULL; if( This. Boxed = =NULL) {Boxed= This. CreateValue (); This. Boxed =boxed; } returnBoxed.value; } //creating an internal class instance PrivateBoxed CreateValue () {//if the delegate that created the object instance valuefactory exists if( This. valuefactory! =NULL) { //To generate an object instance from a delegate return NewBoxed ( This. valuefactory ()); } Else { //Otherwise, the object instance is generated by reflection return NewBoxed ((T) Activator.CreateInstance (typeof(T))); } } //inner nested class, which assigns values to its fields through constructors Private classBoxed {InternalT value; InternalBoxed (T value) { This. Value =value; } } }
Customize the class with constructors.
Public class Big { publicintgetset;} Public Big (int ID) { this. id = ID; } }
Customizes the factory class that created the object instance.
Public class bigfactory { publicstatic Big Build () { returnnew Big ( Ten ); } }
Called by the client.
class program { staticvoid Main (string[] args) { mylazyNew Mylazy<big> (() = bigfactory.build ()); Console.WriteLine (temp. value.id); Console.readkey (); } }
The nature of lazy loading is roughly:
Generating an object instance from the internal nested class of a deferred load class
Delays the acquisition of an object instance by delaying loading a property of the class, and the object instance is created by means of a delegate, etc.
Resources:
You must know. NET (2nd edition), author Wang Tao.
". NET GENERIC "series includes:
. NET generics 01, why generic, generic basic syntax is required. NET generics 02, the use of generics. NET generics 03, conversion of generic types, covariance and contravariance. NET generics 04, using lazy<t> for lazy loading