Sometimes it makes sense to lazily initialize something in an application. In the case of an application internal cache waiting until the first access of data to prime the cache could improve startup times. One way of lazy initialization with C# is Lazy<T>.


Per the default is thread safe, but can be disabled. If you don’t opt-out it’s thread safe.

using System;

public class Program
	public static void Main()
        // Uses LazyThreadSafetyMode.ExecutionAndPublication
        // See
		var lazyString = new Lazy<string>(() => "abc");

		Console.WriteLine(lazyString.Value); // abc

Thread safety can be adjusted like

var lazyString = new Lazy<string>(() => "abc", isThreadSafe: false);


// Add:
// using System.Threading;

var lazyString1 = new Lazy<string>(() => "abc", LazyThreadSafetyMode.None);
var lazyString2 = new Lazy<string>(() => "abc", LazyThreadSafetyMode.PublicationOnly);


Different LazyThreadSafetyMode values have characteristics you may be interested in:

  • LazyThreadSafetyMode.ExecutionAndPublication can deadlock if the initialization code has locks internally. It is also the default value.
  • LazyThreadSafetyMode.None can help for high performance scenarios when there’s a guarantee it won’t be called simultaneously by multiple threads.
  • LazyThreadSafetyMode.Publication allows multiple executions of the initialization code by multiple threads. The first thread to complete the initialization code sets the value.


In some cases an unhandled exception in the lazy initialization code will also cache returning an exception. See which contains a table describing when this occurs.

Additional info

If you’re looking to explore further try these docs pages: