This method helps improve the performance of applications, avoid wasting computing resources, and save memory usage. In this way, it seems that it is more appropriate to create an out-of-the-box instance.
First, let's take a look at how to implement delayed loading in Framework4.0.
Framework4.0 provides a packaging class Lazy, which can easily implement delayed loading.
Copy codeThe Code is as follows: // This line of code indicates that you want to create a String object s with delayed loading.
/// The prototype is LazyT> Object Name = new LazyT> (FunT>)
/// Use a generic delegate for construction. The T-type method must be returned when the delegate is instantiated.
/// In this example, if T is string, the return value of TestLazy. GetString must be of the string type.
Lazystring> s = new Lazystring> (TestLazy. GetString );
In this example, the TestLazy. GetString () method is as follows:
Public class TestLazy
{
Public static string GetString ()
{
Return DateTime. Now. ToLongTimeString ();
}
}
You can use the IsValueCreated attribute to determine whether an object has been created and use the Value attribute to obtain the Value of the current object.
Console. WriteLine (s. IsValueCreated); // return False
Console. WriteLine (s. IsValueCreated); // return True
The complete code is provided below for testing:Copy codeThe Code is as follows: class Program
{
Static void Main (string [] args)
{
/// This line of code indicates that you want to create a delayed loading String object s
/// The prototype is Lazy object name = new Lazy (Fun)
/// Use a generic delegate for construction. The T-type method must be returned when the delegate is instantiated.
/// In this example, if T is string, the return value of TestLazy. GetString must be of the string type.
Lazy s = new Lazy (TestLazy. GetString );
Console. WriteLine (s. IsValueCreated); // return False
Console. WriteLine (s. IsValueCreated); // return True
}
}
Public class TestLazy
{
Public static string GetString ()
{
Return DateTime. Now. ToLongTimeString ();
}
}
The following is an example to demonstrate delayed loading:
In this example, the BlogUser object is used. This object contains multiple Article objects. When the BlogUser object is loaded, the Article object is not loaded. It is loaded only when the Article object is used.Copy codeThe Code is as follows: class Program
{
Static void Main (string [] args)
{
BlogUser blogUser = new BlogUser (1 );
Console. WriteLine ("blogUser has been initialized ");
{
Console. WriteLine (article. Title );}
}
}
Public class BlogUser
{
Public int Id {get; private set ;}
Public Lazy> Articles {get; private set ;}
Public BlogUser (int id)
{
This. Id = id;
Articles = new Lazy> () => ArticleServices. GetArticesByID (id ));
Console. WriteLine ("BlogUser Initializer ");
}
}
Public class Article
{
Public int Id {get; set ;}
Public string Title {get; set ;}
Public DateTime PublishDate {get; set ;}
Public class ArticleServices
{
Public static List GetArticesByID (int blogUserID)
{
List articles = new List {
New Article {Id = 1, Title = "Lazy Load", PublishDate = DateTime. Parse ("2011-4-20 ")},
New Article {Id = 2, Title = "Delegate", PublishDate = DateTime. Parse ("2011-4-21 ")},
New Article {Id = 3, Title = "Event", PublishDate = DateTime. Parse ("2011-4-22 ")},
New Article {Id = 4, Title = "Thread", PublishDate = DateTime. Parse ("2011-4-23}
};
Console. WriteLine ("Article Initalizer ");
Return articles;
}
}
Running result:
Finally, the main application scenarios of delayed loading are as follows:
When the sub-object overhead of an object is large, and this sub-object may not be used in the program, you can consider using delayed loading to create the sub-object. Another scenario is that when the program starts, multiple objects need to be created, but only a few objects need to be used immediately, so that unnecessary initialization work can be delayed for use, this can effectively increase the startup speed of the program.
This technology is widely used in the ORM framework and is not unique to C #. For example, the Hibernate framework in Java also uses this technology.