LazyInitializer.EnsureInitialized Method

Definition

Initializes a target type if it hasn't already been initialized.

Overloads

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>)

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

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, Boolean, Object, Func<T>)

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

EnsureInitialized<T>(T)

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

public static T EnsureInitialized<T> (ref T target) where T : class;
Type Parameters
T

The type of the reference to be initialized.

Parameters
target
T

A reference of type T to initialize if it has not already been initialized.

Returns
T

The initialized reference of type T.

Exceptions

Permissions to access the constructor of type T were missing.

Type T does not have a default constructor.

Remarks

This method may only be used on reference types. To ensure initialization of value types, see other overloads of EnsureInitialized.

This method may be used concurrently by multiple threads to initialize target.

In the event that multiple threads access this method concurrently, multiple instances of T may be created, but only one will be stored into target. In such an occurrence, this method will not dispose of the objects that were not stored. If such objects must be disposed, it is up to the caller to determine if an object was not used and to then dispose of the object appropriately.

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

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

public static T EnsureInitialized<T> (ref T target, Func<T> valueFactory) where T : class;
Type Parameters
T

The reference type of the reference to be initialized.

Parameters
target
T

The reference of type T to initialize if it hasn't already been initialized.

valueFactory
Func<T>

The function that is called to initialize the reference.

Returns
T

The initialized value of type T.

Exceptions

Type T does not have a default constructor.

valueFactory returned null (Nothing in Visual Basic).

Remarks

This method may only be used on reference types, and valueFactory may not return a null reference (Nothing in Visual Basic). To ensure initialization of value types or to allow null reference types, see other overloads of EnsureInitialized.

This method may be used concurrently by multiple threads to initialize target.

In the event that multiple threads access this method concurrently, multiple instances of T may be created, but only one will be stored into target. In such an occurrence, this method will not dispose of the objects that were not stored. If such objects must be disposed, it is up to the caller to determine if an object was not used and to then dispose of the object appropriately.

EnsureInitialized<T>(T, Boolean, Object)

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

public static T EnsureInitialized<T> (ref T target, ref bool initialized, ref object syncLock);
Type Parameters
T

The type of the reference to be initialized.

Parameters
target
T

A reference or value of type T to initialize if it hasn't already been initialized.

initialized
Boolean

A reference to a Boolean value that determines whether the target has already been initialized.

syncLock
Object

A reference to an object used as the mutually exclusive lock for initializing target. If syncLock is null, a new object will be instantiated.

Returns
T

The initialized value of type T.

Exceptions

Permissions to access the constructor of type T were missing.

Type T does not have a default constructor.

Remarks

If intialized is specified as true, then no further initialization occurs.

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

public static T EnsureInitialized<T> (ref T target, ref object syncLock, Func<T> valueFactory) where T : class;
Type Parameters
T
Parameters
target
T
syncLock
Object
valueFactory
Func<T>
Returns
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.

public static T EnsureInitialized<T> (ref T target, ref bool initialized, ref object syncLock, Func<T> valueFactory);
Type Parameters
T

The type of the reference to be initialized.

Parameters
target
T

A reference or value of type T to initialize if it hasn't already been initialized.

initialized
Boolean

A reference to a Boolean value that determines whether the target has already been initialized.

syncLock
Object

A reference to an object used as the mutually exclusive lock for initializing target. If syncLock is null, a new object will be instantiated.

valueFactory
Func<T>

The function that is called to initialize the reference or value.

Returns
T

The initialized value of type T.

Exceptions

Permissions to access the constructor of type T were missing.

Type T does not have a default constructor.

Remarks

If intialized is specified as true, then no further initialization occurs.

Applies to