.NET 4.0 is so Lazy

With .NET 4.0 there is a new class added to the System namespace called Lazy<T>. This class is what the name says, lazy. Here is an example where Lazy is used:

var lazy = new Lazy<IList<OrderRow>>(
                                () =>
                                {
                                        var rows = //get order rows;
                                        return rows;
                                });

var rows = lazy.Value;


The Lazy<T>’s constructor can take a Func<T> as an argument, the function passed as an argument to the contractor will first be invoked when the Value property of the Lazy<T> class is used, but not invoked the next time the Value property is used. The code above will first execute the function passed as an argument when the we request the value of the Lazy<T>, the returned value of the function will be cached. The next time Value is used, the function will not be invoked, instead the cached value will be returned. This class  can for example be used when we want some kind of Lazy Loading.

I’m on twitter: http://www.twitter.com/fredrikn

Published Tuesday, November 10, 2009 2:10 PM by Fredrik N
Filed under: , ,

Comments

# re: .NET 4.0 is to Lazy

Tuesday, November 10, 2009 9:08 AM by Paul Manili

That's a neat little feature and would be great for expensive objects.  It's so simple!  Thanks.

# Twitter Trackbacks for .NET 4.0 is to Lazy - Fredrik Norm??n [asp.net] on Topsy.com

Pingback from  Twitter Trackbacks for                 .NET 4.0 is to Lazy - Fredrik Norm??n         [asp.net]        on Topsy.com

# re: .NET 4.0 is to Lazy

Tuesday, November 10, 2009 11:58 AM by TD

too

# re: .NET 4.0 is to Lazy

Tuesday, November 10, 2009 3:43 PM by Ken Egozi

it's cool, but could have been cooler.

how? if you removed the need of .Value

if it was returning a proxy, then the usage would have been as if it's not Lazy, and client code would never have to bother about it.

and a tiny whining=> "var lazy = new Lazy(..." should probably be "var lazy = new Lazy<..."  even though I've never actually seen VS2010 or C#4, I think that it the way to go.

BTW, I wonder how thread safety is achieved within the Lazy class.

# re: .NET 4.0 is to Lazy

Tuesday, November 10, 2009 3:51 PM by Fredrik N

@Ken:

The Lazy<T> also have an argument to make it thread safe.

# re: .NET 4.0 is to Lazy

Tuesday, November 10, 2009 4:23 PM by J

"... **too** lazy"

# re: .NET 4.0 is so Lazy

Tuesday, November 10, 2009 4:45 PM by Fredrik N

@J:

Almost, should be "so", it was a typo. But thanks for pointing it out.

# Dew Drop &#8211; November 11, 2009 | Alvin Ashcraft&#039;s Morning Dew

Wednesday, November 11, 2009 8:53 AM by Dew Drop – November 11, 2009 | Alvin Ashcraft's Morning Dew

Pingback from  Dew Drop &#8211; November 11, 2009 | Alvin Ashcraft&#039;s Morning Dew

# Reflective Perspective - Chris Alcock &raquo; The Morning Brew #475

Pingback from  Reflective Perspective - Chris Alcock  &raquo; The Morning Brew #475

# re: .NET 4.0 is so Lazy

Thursday, November 12, 2009 7:47 AM by SteinarH

Regarding thread-safe:

The default is to be thread-safe, but it can be swithced off if needed:

msdn.microsoft.com/.../dd642331(VS.100).aspx

channel9.msdn.com/.../LazyltT-Optimized-Resource-Initialization

# Get Lazy with .NET 4.0!

Thursday, November 12, 2009 8:04 AM by DotNetShoutout

Thank you for submitting this cool story - Trackback from DotNetShoutout

# Beware of the Lazy&lt;T&gt; .NET 4.0 type. The closure trap. &laquo; Why Can&#039;t You Code?

Pingback from  Beware of the Lazy&lt;T&gt; .NET 4.0 type. The closure trap. &laquo;  Why Can&#039;t You Code?

# re: .NET 4.0 is so Lazy

Friday, November 13, 2009 9:48 AM by syed.tayyab.ali

System.Lazy is new namespace.

Leave a Comment

(required) 
(required) 
(optional)
(required)