One of the pretty cool new nuggets inside the .Net 4.0 framework is System.Lazy and System.Lazy<T>. What System.Lazy brings to the table is a way to create objects which may need to perform intensive operations and defer the execution of the operation until it is 100% absolutely needed.
Lets take a look at how to use the System.Lazy type.
To better convey how to use this type we should first create some context. In our example (trivial i know) we will have an orders factory which creates orders. Take a look at the code below.
Pay attention to the Console.WriteLine in each factory method, these lines will provide us an indication of when the execution of these methods takes place.
Now that we have context, lets look at the example code for System.Lazy
In the code below we are going to call into the order factory and NOT use the values it returns right away.
If we run this console application you will receive the following information:
You will notice that the second I call into the 2 methods they are executed, which is what is expected. Now lets use the .Lazy namespace and see what changes
If we rerun this console application you will receive the following information:
Here you will notice that i have created my points to the methods, but they have not been executed. I know this by taking a look at the .IsValueCreated property on the Lazy type. Since this has returned False I know my pointers have not been executed. However, the second we call .Value you will see the messages appear which tell us the pointers have been executed.
Finally, if i recheck the .IsValueCreated property this time it returns True which tells me we have indeed executed our pointers.
As you can see using Lazy is easy and can be a very powerful tool in your tool chest. But like everything else, this feature of the framework is NOT meant for ever scenario, use it where it makes sense.
Till next time,
05-19-2010 5:51 AM