Edytuj

Udostępnij za pośrednictwem


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 parameterless 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 parameterless constructor if it hasn't already been initialized.

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

Initializes a target reference type with a specified function if it has not already been initialized.

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)

Source:
LazyInitializer.cs
Source:
LazyInitializer.cs
Source:
LazyInitializer.cs

Initializes a target reference type with the type's parameterless 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;
public static T EnsureInitialized<T> (ref T? target) where T : class;
static member EnsureInitialized : 'T -> '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

A reference to initialize if it has not already been initialized. If it is null, it is considered not initialized; otherwise, it's considered initialized.

Returns

T

The initialized object.

Exceptions

Permissions to access the constructor of type T were missing.

Type T does not have a parameterless 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 and returned. In such an occurrence, this method will not dispose of the objects that were not stored. If such objects must be disposed, use an overload that takes a valueFactory and dispose of the object if it does not reference the same stored object.

See also

Applies to

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

Source:
LazyInitializer.cs
Source:
LazyInitializer.cs
Source:
LazyInitializer.cs

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;
public static T EnsureInitialized<T> (ref T? target, Func<T> valueFactory) where T : class;
static member EnsureInitialized : 'T * 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 type of the reference to be initialized.

Parameters

target
T

The reference 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 object.

Exceptions

Type T does not have a parameterless 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.

See also

Applies to

EnsureInitialized<T>(T, Boolean, Object)

Source:
LazyInitializer.cs
Source:
LazyInitializer.cs
Source:
LazyInitializer.cs

Initializes a target reference or value type with its parameterless 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);
public static T EnsureInitialized<T> (ref T target, ref bool initialized, ref object? syncLock);
static member EnsureInitialized : 'T * bool * obj -> '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

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

Exceptions

Permissions to access the constructor of type T were missing.

Type T does not have a parameterless constructor.

Remarks

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

See also

Applies to

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

Source:
LazyInitializer.cs
Source:
LazyInitializer.cs
Source:
LazyInitializer.cs

Initializes a target reference type with a specified function if it has not already been initialized.

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;
public static T EnsureInitialized<T> (ref T target, ref object syncLock, Func<T> valueFactory) where T : class;
static member EnsureInitialized : 'T * obj * 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

The type of the reference to be initialized.

Parameters

target
T

A reference to initialize if it has not already been initialized. If it is null, it is considered not initialized; otherwise, it's considered 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 method to invoke to initialize target.

Returns

T

The initialized object.

Applies to

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

Source:
LazyInitializer.cs
Source:
LazyInitializer.cs
Source:
LazyInitializer.cs

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);
public static T EnsureInitialized<T> (ref T target, ref bool initialized, ref object? syncLock, Func<T> valueFactory);
static member EnsureInitialized : 'T * bool * obj * 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

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

Exceptions

Permissions to access the constructor of type T were missing.

Type T does not have a parameterless constructor.

Remarks

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

See also

Applies to