mscorlib 4.0.0.0 System.Object System.Diagnostics.DebuggerDisplay("ThreadSafetyMode={Mode}, IsValueCreated={IsValueCreated}, IsValueFaulted={IsValueFaulted}, Value={ValueForDebugDisplay}") System.Runtime.InteropServices.ComVisible(false) To be added.

Use lazy initialization to defer the creation of a large or resource-intensive object, or the execution of a resource-intensive task, particularly when such creation or execution might not occur during the lifetime of the program.

To prepare for lazy initialization, you create an instance of . The type argument of the object that you create specifies the type of the object that you want to initialize lazily. The constructor that you use to create the object determines the characteristics of the initialization. Lazy initialization occurs the first time the property is accessed.

In most cases, choosing a constructor depends on your answers to two questions:

Will the lazily initialized object be accessed from more than one thread? If so, the object might create it on any thread. You can use one of the simple constructors whose default behavior is to create a thread-safe object, so that only one instance of the lazily instantiated object is created no matter how many threads try to access it. To create a object that is not thread safe, you must use a constructor that enables you to specify no thread safety.

Making the object thread safe does not protect the lazily initialized object. If multiple threads can access the lazily initialized object, you must make its properties and methods safe for multithreaded access.

Does lazy initialization require a lot of code, or does the lazily initialized object have a default constructor that does everything you need and doesn't throw exceptions? If you need to write initialization code or if exceptions need to be handled, use one of the constructors that takes a factory method. Write your initialization code in the factory method.

The following table shows which constructor to choose, based on these two factors:

Object will be accessed by

If no initialization code is required (default constructor), use

If initialization code is required, use

Multiple threads

One thread

with set to false.

with set to false.

You can use a lambda expression to specify the factory method. This keeps all the initialization code in one place. The lambda expression captures the context, including any arguments you pass to the lazily initialized object's constructor.

Exception caching When you use factory methods, exceptions are cached. That is, if the factory method throws an exception the first time a thread tries to access the property of the object, the same exception is thrown on every subsequent attempt. This ensures that every call to the property produces the same result and avoids subtle errors that might arise if different threads get different results. The stands in for an actual T that otherwise would have been initialized at some earlier point, usually during startup. A failure at that earlier point is usually fatal. If there is a potential for a recoverable failure, we recommend that you build the retry logic into the initialization routine (in this case, the factory method), just as you would if you weren’t using lazy initialization.

Alternative to locking In certain situations, you might want to avoid the overhead of the object's default locking behavior. In rare situations, there might be a potential for deadlocks. In such cases, you can use the or constructor, and specify . This enables the object to create a copy of the lazily initialized object on each of several threads if the threads call the property simultaneously. The object ensures that all threads use the same instance of the lazily initialized object and discards the instances that are not used. Thus, the cost of reducing the locking overhead is that your program might sometimes create and discard extra copies of an expensive object. In most cases, this is unlikely. The examples for the and constructors demonstrate this behavior.

When you specify , exceptions are never cached, even if you specify a factory method.

Equivalent constructors In addition to enabling the use of , the and constructors can duplicate the functionality of the other constructors. The following table shows the parameter values that produce equivalent behavior.

To create a object that is

For constructors that have a LazyThreadSafetyMode parameter, set to

For constructors that have a Boolean parameter, set to

For constructors with no thread safety parameters

Fully thread safe; uses locking to ensure that only one thread initializes the value.

true

All such constructors are fully thread safe.

Not thread safe.

false

Not applicable.

Fully thread safe; threads race to initialize the value.

Not applicable.

Not applicable.

Other capabilities For information about the use of with thread-static fields, or as the backing store for properties, see Lazy Initialization.

Provides support for lazy initialization.

- Constructor
  4.0.0.0

  An instance that is created with this constructor may be used concurrently from multiple threads.

  The thread safety mode of a instance that is initialized with this constructor is . The thread safety mode describes the behavior when multiple threads try to initialize the instance.

  A instance that is created with this constructor does not cache exceptions. For more information, see the class or the enumeration.

  Initializes a new instance of the class. When lazy initialization occurs, the default constructor of the target type is used.

- Constructor
  4.0.0.0

  The thread safety mode of a instance that is initialized with this constructor is if is true; otherwise, the mode is . The thread safety mode describes the behavior when multiple threads try to initialize the instance. To specify the mode, use the or constructor.

  A instance that is created with this constructor does not cache exceptions. For more information, see the class or the enumeration.

  Initializes a new instance of the class. When lazy initialization occurs, the default constructor of the target type and the specified initialization mode are used.

  true to make this instance usable concurrently by multiple threads; false to make the instance usable by only one thread at a time.
- Constructor
  4.0.0.0
  To be added.
  To be added.
  To be added.
- Constructor
  4.0.0.0

  The thread safety mode of a instance describes the behavior when multiple threads try to initialize the instance.

  A instance that is created with this constructor does not cache exceptions. For more information, see the class or the enumeration.

  Initializes a new instance of the class that uses the default constructor of and the specified thread-safety mode.

  One of the enumeration values that specifies the thread safety mode.
- Constructor
  4.0.0.0
  To be added.
  To be added.
  To be added.
  To be added.
- Constructor
  4.0.0.0
  To be added.
  To be added.
  To be added.
  To be added.
- Property
  4.0.0.0
  System.Boolean To be added.

  When lazy initialization occurs for a instance, it may result in either a value being created or an exception being thrown. If an exception is thrown, subsequent behavior of the instance depends on whether exception caching is in effect. If the instance was created by using a constructor that does not specify an initialization function, then exception caching is not in effect. A subsequent attempt to initialize the might succeed, and after successful initialization the property returns true. If the instance was created with an initialization function (specified by the parameter of the constructor), then exception caching is controlled by the thread safety mode.

  If the mode is or , there is no second chance to initialize the instance. If an exception occurs and is unhandled in the initialization function, that exception is cached and rethrown on subsequent accesses of the property. No value is created if an exception is thrown, so in such cases returns false.

  If the mode is , the first thread that succeeds in running the initialization function (or the default constructor) creates the value for the instance. If the initialization function throws an exception on one thread, other threads can still try to initialize the instance. Until the value is created, the property returns false.

  Gets a value that indicates whether a value has been created for this instance.

- Method
  4.0.0.0
  System.String

  Calling this method does not cause initialization.

  The property can be null after lazy initialization if the factory method that was specified for the parameter of the , , or constructor returns null.

  Creates and returns a string representation of the property for this instance.

  The result of calling the method on the property for this instance, if the value has been created (that is, if the property returns true). Otherwise, a string indicating that the value has not been created.

- Property
  4.0.0.0
  System.Diagnostics.DebuggerBrowsable(System.Diagnostics.DebuggerBrowsableState.Never)
  T To be added.

  If the property is false, accessing the property forces initialization.

  In addition to the exceptions that are listed, the property can throw any unhandled exception that is thrown by the factory method that was passed to the parameter of the , , or constructor.

  Gets the lazily initialized value of the current instance.