SIGN UP MEMBER LOGIN:    
ARTICLE

System.Lazy(Of T) Class

Posted by Shinuraj Articles | .NET 4.5 February 16, 2011
With .Net 4.0 we  can enable lazy initialization for any custom type by wrapping the type inside a System.Lazy(Of T) class.
Reader Level:

With lazy initialization, the memory for an object is not allocated until it is needed. Lazy initialization can improve performance by spreading object allocations evenly across the lifetime of a program.

With .Net 4.0 we  can enable lazy initialization for any custom type by wrapping the type inside a System.Lazy(Of T) class.

If you're familiar with the Singleton pattern, you've probably seen lazy initialization in action as well.

   public class SomeClassSingleton {

    private static SomeClass _instance = null;

    private SomeClassSingleton()  {   }

     private static SomeClass GetInstance()

    {

        if(_instance == null)

            _instance = new SomeClassSingleton(); 

        return _instance;

    } }

So instead of writing all these line we can simply use this class getting the same performance.

For example if we have a  Customer object that has an Orders property that contains a large array of Order objects that, to be initialized, requires a database connection. If the user never asks to display the Orders or use the data in a computation, then there is no reason to use system memory or computing cycles to create it.

By using Lazy<Orders> to declare the Orders object for lazy initialization, we can avoid wasting system resources when the object is not used.

// Initialize by using default Lazy<T> constructor. The

// Orders array itself is not created yet.

Lazy<Orders> _orders = new Lazy<Orders>();

 

Thanks

shinu

Login to add your contents and source code to this article
share this article :
post comment
 

You just gave a small snippet to show how lazy keyword is used: Lazy<Orders> _orders = new Lazy<Orders>(); Your article could be more useful if you had provided a working example.

Posted by Vijay Gill Feb 16, 2011
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Become a Sponsor