LazyInitializer.EnsureInitialized LazyInitializer.EnsureInitialized LazyInitializer.EnsureInitialized LazyInitializer.EnsureInitialized Method

Definition

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

Overloads

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.

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.

public:
generic <typename T>
 where T : class static T EnsureInitialized(T % target);
public static T EnsureInitialized<T> (ref T target) where T : class;
static member EnsureInitialized :  -> 'T (requires 'T : null)
Public Shared Function EnsureInitialized(Of T As Class) (ByRef target As T) As T

Type Parameters

T

The type of the reference to be initialized.

Parameters

target
T T T T

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

Returns

T T T T

The initialized reference of type T.

Exceptions

Permissions to access the constructor of type T were missing.

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.

See also

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.

public:
generic <typename T>
 where T : class static T EnsureInitialized(T % target, Func<T> ^ valueFactory);
public static T EnsureInitialized<T> (ref T target, Func<T> valueFactory) where T : class;
static member EnsureInitialized :  * Func<'T (requires 'T : null)> -> 'T (requires 'T : null)
Public Shared Function EnsureInitialized(Of T As Class) (ByRef target As T, valueFactory As Func(Of T)) As T

Type Parameters

T

The reference type of the reference to be initialized.

Parameters

target
T T T 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 T T T

The initialized value of type T.

Exceptions

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.

See also

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.

public:
generic <typename T>
 static T EnsureInitialized(T % target, bool % initialized, System::Object ^ % syncLock);
public static T EnsureInitialized<T> (ref T target, ref bool initialized, ref object syncLock);
static member EnsureInitialized :  *  *  -> 'T
Public Shared Function EnsureInitialized(Of T) (ByRef target As T, ByRef initialized As Boolean, ByRef syncLock As Object) As T

Type Parameters

T

The type of the reference to be initialized.

Parameters

target
T T T T

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

initialized
Boolean Boolean Boolean Boolean

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

syncLock
Object Object Object 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 T T T

The initialized value of type T.

Exceptions

Permissions to access the constructor of type T were missing.

Remarks

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

See also

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

public:
generic <typename T>
 where T : class static T EnsureInitialized(T % target, System::Object ^ % syncLock, Func<T> ^ valueFactory);
public static T EnsureInitialized<T> (ref T target, ref object syncLock, Func<T> valueFactory) where T : class;
static member EnsureInitialized :  *  * Func<'T (requires 'T : null)> -> 'T (requires 'T : null)
Public Shared Function EnsureInitialized(Of T As Class) (ByRef target As T, ByRef syncLock As Object, valueFactory As Func(Of T)) As T

Type Parameters

T

Parameters

target
T T T T
valueFactory
Func<T>

Returns

T T T 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.

public:
generic <typename T>
 static T EnsureInitialized(T % target, bool % initialized, System::Object ^ % syncLock, Func<T> ^ valueFactory);
public static T EnsureInitialized<T> (ref T target, ref bool initialized, ref object syncLock, Func<T> valueFactory);
static member EnsureInitialized :  *  *  * Func<'T> -> 'T
Public Shared Function EnsureInitialized(Of T) (ByRef target As T, ByRef initialized As Boolean, ByRef syncLock As Object, valueFactory As Func(Of T)) As T

Type Parameters

T

The type of the reference to be initialized.

Parameters

target
T T T T

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

initialized
Boolean Boolean Boolean Boolean

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

syncLock
Object Object Object 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 T T T

The initialized value of type T.

Exceptions

Permissions to access the constructor of type T were missing.

Remarks

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

See also

Applies to