Provides support for lazy initialization.
Assembly: mscorlib (in mscorlib.dll)
Thetype exposes the following members.
|Lazy(T)()||Initializes a new instance of the class. When lazy initialization occurs, the default constructor of the target type is used.|
|Lazy(T)(Boolean)||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.|
|Lazy(T)(Func(T))||Initializes a new instance of the class. When lazy initialization occurs, the specified initialization function is used.|
|Lazy(T)(LazyThreadSafetyMode)||Initializes a new instance of the class that uses the default constructor of T and the specified thread-safety mode.|
|Lazy(T)(Func(T), Boolean)||Initializes a new instance of the class. When lazy initialization occurs, the specified initialization function and initialization mode are used.|
|Lazy(T)(Func(T), LazyThreadSafetyMode)||Initializes a new instance of the class that uses the specified initialization function and thread-safety mode.|
|Equals(Object)||Determines whether the specified Object is equal to the current Object. (Inherited from Object.)|
|Finalize||Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)|
|GetHashCode||Serves as a hash function for a particular type. (Inherited from Object.)|
|GetType||Gets the Type of the current instance. (Inherited from Object.)|
|MemberwiseClone||Creates a shallow copy of the current Object. (Inherited from Object.)|
|ToString||Creates and returns a string representation of the Lazy(T).Value property for this instance. (Overrides Object.ToString().)|
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 Lazy(T).Value 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
Lazy(T) with isThreadSafe set to false.
Lazy(T) with isThreadSafe 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 Value property of the object, the same exception is thrown on every subsequent attempt. This ensures that every call to the Value 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 Lazy(T) or Lazy(T) constructor, and specify LazyThreadSafetyMode.PublicationOnly. This enables the object to create a copy of the lazily initialized object on each of several threads if the threads call the Value 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 Lazy(T) and Lazy(T) constructors demonstrate this behavior.
When you specify PublicationOnly, exceptions are never cached, even if you specify a factory method.
Equivalent constructors In addition to enabling the use of PublicationOnly, the Lazy(T) and Lazy(T) 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 mode parameter, set mode to
For constructors that have a Boolean isThreadSafe parameter, set isThreadSafe to
For constructors with no thread safety parameters
Fully thread safe; uses locking to ensure that only one thread initializes the value.
All such constructors are fully thread safe.
Not thread safe.
Fully thread safe; threads race to initialize the value.
Other capabilities For information about the use of with thread-static fields, or as the backing store for properties, see Lazy Initialization.
The HostProtectionAttribute attribute applied to this type or member has the following Resources property value: Synchronization | ExternalThreading. The HostProtectionAttribute does not affect desktop applications (which are typically started by double-clicking an icon, typing a command, or entering a URL in a browser). For more information, see the HostProtectionAttribute class or SQL Server Programming and Host Protection Attributes.
The following example demonstrates the use of the class to provide lazy initialization with access from multiple threads.
The example uses the Lazy(T) constructor. It also demonstrates the use of the Lazy(T) constructor (specifying true for isThreadSafe) and the Lazy(T) constructor (specifying LazyThreadSafetyMode.ExecutionAndPublication for mode). To switch to a different constructor, just change which constructors are commented out.
For an example that demonstrates exception caching using the same constructors, see the Lazy(T) constructor.
The example defines a LargeObject class that will be initialized lazily by one of several threads. The four key sections of code illustrate the creation of the initializer, the factory method, the actual initialization, and the constructor of the LargeObject class, which displays a message when the object is created. At the beginning of the Main method, the example creates the thread-safe lazy initializer for LargeObject:
The factory method shows the creation of the object, with a placeholder for further initialization:
Note that the first two code sections could be combined by using a lambda function, as shown here:
The example pauses, to indicate that an indeterminate period may elapse before lazy initialization occurs. When you press the Enter key, the example creates and starts three threads. The ThreadProc method that's used by all three threads calls the Value property. The first time this happens, the LargeObject instance is created:
The constructor of the LargeObject class, which includes the last key section of code, displays a message and records the identity of the initializing thread. The output from the program appears at the end of the full code listing.
For simplicity, this example uses a global instance of , and all the methods are static (Shared in Visual Basic). These are not requirements for the use of lazy initialization.
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.