Table of Contents

Class EnumerableExtensions

Namespace
AiDotNet.Extensions
Assembly
AiDotNet.dll

Provides extension methods for IEnumerable collections to enhance their functionality.

public static class EnumerableExtensions
Inheritance
EnumerableExtensions
Inherited Members

Remarks

For Beginners: Extension methods are special methods that add new capabilities to existing types without modifying the original code. This class adds useful operations to collections in your code.

Methods

RandomElement<T>(IEnumerable<T>)

Returns a random element from a collection.

public static T RandomElement<T>(this IEnumerable<T> enumerable)

Parameters

enumerable IEnumerable<T>

The collection to select a random element from.

Returns

T

A randomly selected element from the collection. If the collection is empty, returns the numeric zero value for type T.

Type Parameters

T

The type of elements in the collection.

Remarks

For Beginners: This method picks one random item from your collection, similar to drawing a name from a hat. If your collection is empty, it returns zero (or the equivalent for the data type you're using).

The method works with any collection type (arrays, lists, etc.) and handles empty collections safely by returning a default value instead of causing an error.

Example usage:

var numbers = new[] { 1, 2, 3, 4, 5 };
var randomNumber = numbers.RandomElement(); // Might return any number from the array