EnumerableExtensionsGetRandomElementT(IEnumerableT, Boolean) Method

Gets a random element from the enumerable source using a new FastRandom instance.

Definition

Namespace: KGySoft.CoreLibraries
Assembly: KGySoft.CoreLibraries (in KGySoft.CoreLibraries.dll) Version: 9.0.0-preview.1
C#
public static T GetRandomElement<T>(
	this IEnumerable<T> source,
	bool defaultIfEmpty = false
)

Parameters

source  IEnumerableT
The IEnumerableT to select an element from.
defaultIfEmpty  Boolean  (Optional)
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

T
A random element from 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).

See Also