KGy SOFT

EnumerableExtensionsGetRandomElementT Method (IEnumerableT, Random, Boolean)

KGy SOFT Core Libraries Help
Gets a random element from the enumerable source using a specified Random instance.

Namespace:  KGySoft.CoreLibraries
Assembly:  KGySoft.CoreLibraries (in KGySoft.CoreLibraries.dll) Version: 5.0.0
Syntax

public static T GetRandomElement<T>(
	this IEnumerable<T> source,
	Random random,
	bool defaultIfEmpty = false
)

Parameters

source
Type: System.Collections.GenericIEnumerableT
The IEnumerableT to select an element from.
random
Type: SystemRandom
The Random instance to use.
defaultIfEmpty (Optional)
Type: SystemBoolean
If  and source is empty, the default value of T is returned. If , and source is empty, an ArgumentException will be thrown. This parameter is optional.
Default value: .

Type Parameters

T
The type of the elements of source.

Return Value

Type: T
A random element from the source.

Usage Note

In Visual Basic and C#, you can call this method as an instance method on any object of type IEnumerableT. 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).
Exceptions

ExceptionCondition
ArgumentNullExceptionrandom or source is .
ArgumentExceptionsource contains no elements and defaultIfEmpty is .
See Also

Reference