Click here to Skip to main content
13,459,013 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


3 bookmarked
Posted 10 Jun 2012

Double Check Pattern

, 10 Jun 2012
Rate this:
Please Sign up or sign in to vote.
Double check pattern

I just answered a question at SO about lazy loading which involved the double check pattern. It’s a really useful pattern for lazy loading since it hurt performance a lot less than always locking.

I thought that I should share and explain why by using some comments:

public sealed class Lazy<T> where T : class
    private readonly object _syncRoot = new object();
    private readonly Func<T> _factory;
    private T _value;

    public Lazy(Func<T> factory)
        if (factory == null) throw new ArgumentNullException("factory");

        _factory = factory;

    public T Value
            // here is the first check. It only returns true
            // if the instance have not been created, right?
            if (_value == null)
                // so when we enter here, we are within a very small time frame:
                // That is from the above check until the new instance is
                // assigned to the variable.

                // Which is a <strong>very</strong> small time frame
                // (unless you rely on external resources).

                // So let's lock to make sure that no other thread
                // have already started to create the object.
                lock (_syncRoot)
                    // We enter here as soon as any other thread (if there were one)
                    // have stopped working, which means that the value could
                    // have been assigned.

                    // So let's check if another thread have already done the work for us.
                    if (_value == null)
                        //no, so let's create the object.
                        _value = _factory();
            return _value;

    public override string ToString()
        return _value == null ? "Not created" : _value.ToString();

The double check pattern allows us to have lock free code (other than when the instance is created) which is a huge performance gain compared to using a single lock.

Feel free to use the code in .NET versions earlier than 4.


This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)


About the Author

Founder 1TCompany AB
Sweden Sweden
Skip logfiles, try automated error handling!

I'm one of the founders of codeRR, a .NET service which takes care of everything related to exceptions, so that you can focus on writing code.

blog | twitter

You may also be interested in...

Comments and Discussions

SuggestionSuggestion? Pin
Prooskalia10-Jun-12 9:52
memberProoskalia10-Jun-12 9:52 
GeneralRe: Suggestion? Pin
William E. Kempf15-Jun-12 5:53
memberWilliam E. Kempf15-Jun-12 5:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02-2016 | 2.8.180323.1 | Last Updated 10 Jun 2012
Article Copyright 2012 by jgauffin
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid