Using Lazy<T> and abstract wrapper class to lazy-load complex system parameters

.NET Framework 4.0 introduced new class called Lazy<T> and I wrote blog post about it: .Net Framework 4.0: Using System.Lazy<T>. One thing is annoying for me – we have to keep lazy loaded value and its value loader as separate things. In this posting I will introduce you my Lazy<T> wrapper for complex to get system parameters that uses template method to keep lazy value loader in parameter class.


NB! This blog is moved to

Click here to go to article


  • I'd prefer this:

    public class LazyParameter {
    private readonly Lazy _lazyParam;

    public LazyParameter( Func load ) {
    _lazyParam = new Lazy( load );

    public T Value {
    get { return _lazyParam.Value; }

    Which is a bit cleaner to use:

    var temp = new LazyParameter( () => 1 + 30 ).Value;

    var page = new LazyParameter(
    () => HttpContext.Current.Request.QueryString["page"]

  • Thanks for feedback, AndyT! Your solution is fine when there is not much code in method that retrieves actual value. But in the case of complex logic that needs more code my solution works better because you have separate class with its own methods.

  • But Isent the point of lazy loading data the data is ökades when/if the gode hits the parameter?

    This code loars the data in the constructor?

    How skuld this be used in a buissnes class?

  • Ökades would be loaded in english :)

  • This code doesn't load data in constructor. Lazy accepts Func as constructor argument but it doesn't run this Func immediately.

    When Value is asked then Lazy checks of data is already loaded and if it isn't then it runs this Func to get data.

Comments have been disabled for this content.