LazyInitializer LazyInitializer LazyInitializer LazyInitializer Class

Definition

Provides lazy initialization routines.

public ref class LazyInitializer abstract sealed
public static class LazyInitializer
type LazyInitializer = class
Public Class LazyInitializer
Inheritance
LazyInitializerLazyInitializerLazyInitializerLazyInitializer

Examples

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)  
ExpensiveData _data = null;  
                     bool _dataInitialized = false;  
                     object _dataLock = new object();  
                  //  ...  

                    ExpensiveData dataToUse = LazyInitializer.EnsureInitialized(ref _data, ref _dataInitialized, ref _dataLock);  

Remarks

These routines avoid needing to allocate a dedicated, lazy-initialization instance, instead using references to ensure targets have been initialized as they are accessed.

Methods

EnsureInitialized<T>(T) EnsureInitialized<T>(T) EnsureInitialized<T>(T) EnsureInitialized<T>(T)

Initializes a target reference type with the type's default constructor if it hasn't already been initialized.

EnsureInitialized<T>(T, Func<T>) EnsureInitialized<T>(T, Func<T>) EnsureInitialized<T>(T, Func<T>) EnsureInitialized<T>(T, Func<T>)

Initializes a target reference type by using a specified function if it hasn't already been initialized.

EnsureInitialized<T>(T, Boolean, Object) EnsureInitialized<T>(T, Boolean, Object) EnsureInitialized<T>(T, Boolean, Object) EnsureInitialized<T>(T, Boolean, Object)

Initializes a target reference or value type with its default constructor if it hasn't already been initialized.

EnsureInitialized<T>(T, Object, Func<T>) EnsureInitialized<T>(T, Object, Func<T>) EnsureInitialized<T>(T, Object, Func<T>) EnsureInitialized<T>(T, Object, Func<T>)
EnsureInitialized<T>(T, Boolean, Object, Func<T>) EnsureInitialized<T>(T, Boolean, Object, Func<T>) EnsureInitialized<T>(T, Boolean, Object, Func<T>) EnsureInitialized<T>(T, Boolean, Object, Func<T>)

Initializes a target reference or value type by using a specified function if it hasn't already been initialized.

Applies to

Thread Safety

The methods of LazyInitializer are thread-safe and may be called from multiple threads concurrently.

See Also