Lazy<T>: On Demand Construction in .NET 4.0

I love it when I find those small new bits of functionality in the .NET framework. It’s all the big items that get all the love at conferences and in magazines.

Lazy<T> is one of those items.

Let’s imagine you’re writing an application and in some scenarios, but not others,  you need a particular object. Furthermore, suppose that object you need is very expensive to create and use. You don’t want to create it everytime your application runs. You only want to create it when you need it.

Sure, you could manage all that yourself, but Lazy<T> makes it easy.  You just create a lazy wrapper on the expensive object:

Lazy<ExpensiveResource> ownedResource = new Lazy<ExpensiveResource>();

You can simply reference ‘ownedResource.Value’ to get at the expensive object. The first time you access ownedResource.Value, the expensive resource will get allocated, but not before.

Lazy<T> also has a boolean propoerty, named IsValueCreated, that you can check to see if the value has been created. That would be useful when you need to store information from the expensive resource, but only if it’s been used.

Supporting Types without default constructors

Lazy<T> does not enforce the new() constraint. You can use Lazy<T> for types that must be instantiated using a different constructor, or even a factory method. A second constructor specifies a Func<T> that returns the new expensive resource:

Lazy<ExpensiveResource> ownedResource = new Lazy<ExpensiveResource>(
    () => new ExpensiveResource("filename.data"));

You can use this second constructor to better control what code creates the expensive resource. I’ve used a different constructor here, but you could use a factory method, an IOC container, or any method.

We’re living in a Multi Core World

There are two other constructors in Lazy<T>:

public Lazy(bool isThreadSafe);
public Lazy(Func<T> valueFactory, bool isThreadSafe);

 

These two constructors indicate that you are running in a multi-threaded envrionment, and the lazy construction of of the owned object must be synchronized. (After all, it’s an expensive resource. You don’t want two of them.)

It’s a simple type, but it’s one of those types you’ll find yourself reaching for over and over again.

I’m glad it’s been added.

Published 30 September 2009 07:32 PM by wwagner
Ads by Lake Quincy Media

Comments

# Roland said on 01 October, 2009 05:30 AM

Hm maybe I'm slow but I don't really see how this is different from a good-old Factory

# progg.ru said on 01 October, 2009 09:58 AM

Thank you for submitting this cool story - Trackback from progg.ru

# Chris Brandsma said on 01 October, 2009 11:30 AM

Thank you.  That is one of the first things in .net 4.0 to get me excited.  (dynamic doesn't do that for me).

# wwagner said on 01 October, 2009 09:05 PM

Roland,

The usage is a bit different: Lazy<T> will call the factory method whenever code first needs to access the value. With a plain old factory, you need to manage when the resource is created (by calling hte facory at the proper time). It's not rocket science, but it is useful.

# (Another) Roland said on 02 October, 2009 05:03 AM

Nice! I often use lazy initialization to speed up startup of desktop apps. Not often enough that I'd spend time on writing a helper class that I'd had to carry around from project to project - a code snippet for lazy properties does the job as well. But nice to see that in .NET 4.0 it's simply a part of the framework.

# Roland said on 02 October, 2009 09:53 AM

Ah I see now, missed that :) Can come in quite handy, hope it will be on CF

# andyclap said on 02 October, 2009 12:14 PM

It looks like a generic singleton factory to me, not a proper proxy (which is what Lazy implies).

More importantly it creates a dependency between the caller and T. Why should the caller be responsible for knowing the best way to create a T?

I thought everybody was using DI now.

# Sanjeev Agarwal said on 03 October, 2009 04:08 AM

Daily tech links for .net and related technologies - October 2-4, 2009 Web Development Special Folders

# Noah Coad said on 04 October, 2009 12:29 AM

cool new addition, thanks for sharing Bill!

# Raghuraman said on 07 October, 2009 10:15 PM

That is really cool.

Thanks for posting a good and simple explanation

# Code Monkey Labs said on 12 October, 2009 10:59 AM

Pick of the week: An Engineer's Guide to Bandwidth General Prefer Dependency Injection to Service Location : Steven Harman explains the difference between two common practices for using Inversion of Control containers and shares his opinion why one is

Search

Go

Blog Group Links

Nascar style badges