LazyInitializer Class
[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]
Provides lazy initialization routines.
Assembly: mscorlib (in mscorlib.dll)
| Name | Description | |
|---|---|---|
![]() ![]() | EnsureInitialized<T>(T%) | Initializes a target reference type with the type's default constructor if the target has not already been initialized. |
![]() ![]() | EnsureInitialized<T>(T%, Func<T>) | Initializes a target reference type using the specified function if it has not already been initialized. |
![]() ![]() | EnsureInitialized<T>(T%, Boolean%, Object%) | Initializes a target reference or value type with its default constructor if it has not already been initialized. |
![]() ![]() | EnsureInitialized<T>(T%, Boolean%, Object%, Func<T>) | Initializes a target reference or value type with a specified function if it has not already been initialized. |
The following example demonstrates how to use EnsureInitialized to lazily initialize a value using a Boolean value to track whether initialization has already happened and an object to use as the mutual exclusion lock.
Dim _data As ExpensiveData = Nothing Dim _dataInitialized As Boolean = False Dim _dataLock As Object = Nothing ' ... Dim name = LazyInitializer.EnsureInitialized(_data, _dataInitialized, _dataLock)
[C#]
ExpensiveData _data = null;
bool _dataInitialized = false;
object _dataLock = new object();
// ...
ExpensiveData dataToUse = LazyInitializer.EnsureInitialized(ref _data, ref _dataInitialized, ref _dataLock);
Show:

