Skip to content

Commit

Permalink
FirstOrOptional extension fix to return Option.None instead of defaul…
Browse files Browse the repository at this point in the history
…t on value type enumerable (#460)

* FirstOrOptional fix to return Option.None instead of default on value types enumerable
* return is operator

Co-authored-by: Gleb Bakanov <[email protected]>
  • Loading branch information
Gleb Bakanov and Gleb Bakanov authored Mar 8, 2021
1 parent d08495c commit 0c216b1
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/DynamicData/Kernel/OptionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,15 @@ public static Optional<T> FirstOrOptional<T>(this IEnumerable<T> source, Func<T,
throw new ArgumentNullException(nameof(source));
}

var result = source.FirstOrDefault(selector);
return !Equals(result, null) ? result : Optional.None<T>();
foreach (T item in source)
{
if (selector(item))
{
return Optional<T>.Create(item);
}
}

return Optional.None<T>();
}

/// <summary>
Expand Down

0 comments on commit 0c216b1

Please sign in to comment.