Home > C# > Easy lazy loading

Easy lazy loading

A common use of lazy loading make use this snippet:

public Thing Something {
    get {
            if( something == null )
                    something = new Something();
            return something;

Sometimes, you need to lazy load something inside your class. I mean, a private lazy field. But as c# doesn’t allow us to make a field as “lazy”, so you need to use the previous snippet again, but for a private property, making your code more ugly each time. So I tried to improve the use of this “pattern”.

What I wanted in the first place is to change the previous code into:

private Lazy<Thing> thing;


thing = Lazy.Load<Thing>(); 
thing = Lazy.Load<Thing>( thing => thing.Id = 123); 
thing = Lazy.Load( () => new Thing() );

I know, it could implement an interface and I could make use of dynamic proxies, but the idea behind this is to get to a simple solution.

Anyway, it was pretty easy to have that working. I add a “Instance” property to Lazy class and there I put a little if to check if the instance was loaded. I know, that name reminds to the singleton (anti)pattern, but I couldn’t think any better.

public T Instance {
    get {
            if( !loaded ){
                 instance = load();
                 loaded = true;
            return something;

where load is the delegate used to create the actual instance of the class.

Then I decide that I don’t like that if to be called every time, so I changed it so Instance call a getInstance() delegate that is assigned in the Lazy class constructor:

private T instance;

private T getLoadedInstance() {
	return instance;

private T loadInstance() {
	instance = load();
	getInstance = getLoadedInstance;
	return instance;

public Lazy(Func<T> load) {
	this.load = load;
	getInstance = loadInstance;

private Func<T> getInstance;

public T Instance {
	get {
		return getInstance();

The first time that Instance is called, it calls the load method and the changes the getInstance delegate itself in order to return the created instance.

This is the final code (I wanted to upload it as an attachment but wordpress doesn’t allow zips nor source code files).
Edit: You could download the source code from http://ivowiblo.googlecode.com/svn/Easy%20Lazy%20Loading/

Categories: C# Tags: ,
  1. Emiliano
    January 27, 2010 at 12:17 am

    I like it!

    The only issue I found is the use of Lazy.Instance gets dirty the class code.
    I’d prefer a dinamyc proxy instead of composition… looks better, but it needs some reflection or a framework and it’s overengineering.

    So your solution is nice, but only in the case you have a lot of lazy load of different things and there arent an underlying concept hidden.

  2. Lenny
    February 1, 2010 at 8:03 pm

    I think someone from Microsoft has been spying this blog and added this to .NET 4.0

    • ivowiblo
      February 1, 2010 at 8:13 pm

      WOW! That was actually weird.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: