PkmnLib.NET/PkmnLib.Static/Utils/EnumerableHelpers.cs

36 lines
970 B
C#
Raw Normal View History

2024-07-28 12:00:26 +00:00
namespace PkmnLib.Static.Utils;
/// <summary>
/// Extension methods for <see cref="IEnumerable{T}"/>.
/// </summary>
2024-07-28 12:00:26 +00:00
public static class EnumerableHelpers
{
/// <summary>
/// Returns all elements of the enumerable that are not null.
/// </summary>
2024-07-28 12:00:26 +00:00
public static IEnumerable<T> WhereNotNull<T>(this IEnumerable<T?> enumerable) where T : class =>
enumerable.Where(x => x is not null)!;
2025-03-02 16:19:57 +00:00
2024-08-10 09:18:10 +00:00
/// <summary>
/// Finds the index of a value in an enumerable. Returns -1 if the value is not found.
/// </summary>
public static int IndexOf<T>(this IEnumerable<T> enumerable, T value)
{
var index = 0;
foreach (var item in enumerable)
{
if (item is null)
{
if (value is null)
return index;
continue;
}
if (item.Equals(value))
return index;
index++;
}
return -1;
}
2024-07-28 12:00:26 +00:00
}