This article from: http://www.cnblogs.com/zhangpengshou/archive/2012/12/10/2811765.html
The. NET Framework 4 was finally released over and over again, and at an occasional opportunity I saw Anytao's [you must know. NET] 33rd, deep into the,lazy<t> of. NET 4.0.
I have not seen the implementation of the lazy<t> in the. NET Framework 4.0 Beta2, nor do I know whether the official version and the previous version have improved (changed), and I am only here to talk purely in the. NET Framework 4 lazy<t> The implementation.
1. Lazy<t> Overview
We might encounter a situation where we have a big guy (big object) that needs to be created, and it takes a long time to create the object, and it also needs to allocate more space on the managed heap.
So in the. NET Framework 4 There is such a clever way of:lazy<t> (which we can call lazy objects). Of course, many people have made their own implementations before.
So here we can summarize the role of lazy<t> as a sentence, delayed loading on demand .
2. Use of lazy<t>
Understanding the role of lazy<t>, let's look at how lazy<t> is applied.
Class Program
{
static void Main (string[] args)
{
lazy<large> lazyobject = new lazy<large> ();
Console.WriteLine (lazyobject.isvaluecreated);
LazyObject.Value.Test ();
Console.WriteLine (lazyobject.isvaluecreated);
}
}
[Serializable]
Class Large
{
Public Large () {}
{
Console.WriteLine ("Test");
}
}
The results of the operation are as follows:
False
Test
True
Please press any key to continue ...
This example is very simple, is also the most basic lazy<t>, is also the most common application way.
3. Realize your lazy<t>
Before the. NET Framework 4.0, large objects are present, so how do you deal with a large object for a large system? In my opinion there are two points: lazy loading and instant cleanup. The former solves the creation problem, the latter solves the recycling problem.
So before we look at Lazy<t> 's. NET framework implementations, let's start by implementing a simple lazy<t>.
Class mylazy<t> where T:new ()
{
private T value;
private bool isLoaded;
Public Mylazy ()
{
isLoaded = false;
}
Public T Value
{
Get
{
if (!isloaded)
{
Value = new T ();
IsLoaded = true;
}
return value;
}
}
}
This should be the simplest version of the lazy<t>, no thread-safe detection, in fact, nothing, only the access to create real objects, but for our general application may be enough.
4. lazy<t>. NET Framework Implementations
Originally also want to explain the code, but too much, explain not move ... write some main.
In fact, the. NET framework is similar to the above implementations, with two main differences:
A. Introduction of the boxed inner class:
[Serializable]
Private Class Boxed
{
Fields
Internal T m_value;
Methods
[Targetedpatchingoptout ("Performance critical to-inline this type of method across NGen image boundaries")]
Internal Boxed (T value)
{
This.m_value = value;
}
}
This inner class replaces the generic constraint I made in the above implementation, making it more generic.
However, we should also note that if T is a struct, then because T is very large, it may be a more cost-effective thing to unboxing, so it is recommended that you use lazy<t> for value types sparingly.
B. Thread-Safe control
In the control options for thread security, the. NET Framework provides us with such enumeration options:
public enum LazyThreadSafetyMode
{
None,
Publicationonly,
Executionandpublication
}
Do not make redundant explanations, the specific meaning of these three, MSDN has been very clear, can participate here.
The code inside is more troublesome, not much to say.
5. Perfect solution for large objects
In the reply to the Anytao article, I mentioned one point:
Personally think that Lazy+weakreference is the realization of a large object of the complete solution, an on-demand loading, an indefinite cleanup .....
It's perfect to add together.
But Lao Zhao said:
Not all big objects need to be cleaned automatically ...
Then I will not repeat to summarize, these two words add together, I think should be able to come up with a relatively perfect large-object processing plan it.
If so, can we create a separate implementation class:weaklazy<t>?
This is equivalent to We have weakreference, with Lazy<t>, and Weaklazy<t>, then we can be very complete according to the circumstances of the choice of application!
6. Summary
Originally wanted to write something slightly deeper, but wrote to the end, found still fur, and sigh.
Transferred from: http://www.cnblogs.com/kym/archive/2010/02/21/1670226.html
C # lazy<t> (GO)