EnumerableExtensionsGetRandomElementT(IEnumerableT, Random, Boolean) Method
Gets a random element from the enumerable
source using a specified
Random instance.
Namespace: KGySoft.CoreLibrariesAssembly: KGySoft.CoreLibraries (in KGySoft.CoreLibraries.dll) Version: 9.0.0
public static T GetRandomElement<T>(
this IEnumerable<T> source,
Random random,
bool defaultIfEmpty = false
)
<ExtensionAttribute>
Public Shared Function GetRandomElement(Of T) (
source As IEnumerable(Of T),
random As Random,
Optional defaultIfEmpty As Boolean = false
) As T
public:
[ExtensionAttribute]
generic<typename T>
static T GetRandomElement(
IEnumerable<T>^ source,
Random^ random,
bool defaultIfEmpty = false
)
[<ExtensionAttribute>]
static member GetRandomElement :
source : IEnumerable<'T> *
random : Random *
?defaultIfEmpty : bool
(* Defaults:
let _defaultIfEmpty = defaultArg defaultIfEmpty false
*)
-> 'T
- source IEnumerableT
- The IEnumerableT to select an element from.
- random Random
- The Random instance to use.
- 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: .
- T
- The type of the elements of source.
TA random element from the
source.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).