RandomInteropExtensions.TryUnwrap Method

Definition

Namespace: Numerics.NET.Random
Assembly: Numerics.NET (in Numerics.NET.dll) Version: 10.3.0

Overload List

TryUnwrap(Random, IRandomSource) Attempts to unwrap a Random to recover the underlying random source.
TryUnwrap<T>(Random, T) Attempts to unwrap a Random to recover the underlying random source with the exact specified type.

TryUnwrap(Random, IRandomSource)

Attempts to unwrap a Random to recover the underlying random source.
C#
public static bool TryUnwrap(
	this Random rng,
	out IRandomSource numerics
)

Parameters

rng  Random
The Random to unwrap.
numerics  IRandomSource
When this method returns, contains the underlying random source if rng is a wrapper; otherwise, null.

Return Value

Boolean
true if rng is a WrappedIRandom<TRng> and the underlying RNG was successfully recovered; otherwise, false.

Usage Note

In Visual Basic and C#, you can call this method as an instance method on any object of type Random. When you use instance method syntax to call this method, omit the first parameter. For more information, see Extension Methods (Visual Basic) or Extension Methods (C# Programming Guide).

Remarks

This method attempts to recover the original random source from a Random wrapper created by AsRandom(IRandomSource) or AsRandom<T>(T).

Unwrapping is type-based and conservative. It succeeds only if rng is a recognized wrapper type.

TryUnwrap<T>(Random, T)

Attempts to unwrap a Random to recover the underlying random source with the exact specified type.
C#
public static bool TryUnwrap<T>(
	this Random random,
	out T randomSource
)
where T : class, IRandomSource

Parameters

random  Random
The Random to unwrap.
randomSource  T
When this method returns, contains the underlying random source if random is a wrapper with matching type; otherwise, null.

Type Parameters

T
The expected random source type, which must be a class implementing IRandomSource.

Return Value

Boolean
true if random is a WrappedIRandom<TRng> and the underlying RNG has type T; otherwise, false.

Usage Note

In Visual Basic and C#, you can call this method as an instance method on any object of type Random. When you use instance method syntax to call this method, omit the first parameter. For more information, see Extension Methods (Visual Basic) or Extension Methods (C# Programming Guide).

Remarks

This method attempts to recover the original random source from a Random wrapper created by AsRandom<T>(T).

Unwrapping succeeds only if:

  • random is a WrappedIRandom<T>, and
  • The underlying random source has exactly type T.

See Also