DotNext by .NET Foundation and Contributors

<PackageReference Include="DotNext" Version="4.6.1" />

.NET API 538,048 bytes

 Predicate

public static class Predicate
Provides extension methods for type Predicate<T> and predefined predicates.
using System; using System.Runtime.CompilerServices; namespace DotNext { [System.Runtime.CompilerServices.NullableContext(1)] [System.Runtime.CompilerServices.Nullable(0)] public static class Predicate { private static class TruePredicate<T> { internal static readonly Predicate<T> Value = AlwaysTrue; private static bool AlwaysTrue(T value) { return true; } } private static class FalsePredicate<T> { internal static readonly Predicate<T> Value = AlwaysFalse; private static bool AlwaysFalse(T value) { return false; } } private static class IsNullPredicate<T> where T : class { internal static readonly Predicate<T> Value = ObjectExtensions.IsNull; } private static class IsNotNullPredicate<T> where T : class { internal static readonly Predicate<T> Value = ObjectExtensions.IsNotNull; } private static class HasValuePredicate<T> where T : struct { internal static readonly Predicate<T?> Value = HasValue; private static bool HasValue(T? nullable) { return nullable.HasValue; } } private static class TypeChecker<T> { internal static readonly Predicate<object> Value = ObjectExtensions.IsTypeOf<T>; } [System.Runtime.CompilerServices.NullableContext(2)] [return: System.Runtime.CompilerServices.Nullable(new byte[] { 1, 2 })] public static Predicate<object> IsTypeOf<T>() { return TypeChecker<T>.Value; } public static Predicate<T> IsNull<T>() where T : class { return IsNullPredicate<T>.Value; } public static Predicate<T> IsNotNull<T>() where T : class { return IsNotNullPredicate<T>.Value; } [System.Runtime.CompilerServices.NullableContext(0)] [return: System.Runtime.CompilerServices.Nullable(new byte[] { 1, 0 })] public static Predicate<T?> HasValue<T>() where T : struct { return HasValuePredicate<T>.Value; } public static Predicate<T> True<[System.Runtime.CompilerServices.Nullable(2)] T>() { return TruePredicate<T>.Value; } public static Predicate<T> False<[System.Runtime.CompilerServices.Nullable(2)] T>() { return FalsePredicate<T>.Value; } public static Func<T, bool> AsFunc<[System.Runtime.CompilerServices.Nullable(2)] T>(this Predicate<T> predicate) { return predicate.ChangeType<Func<T, bool>>(); } public static Converter<T, bool> AsConverter<[System.Runtime.CompilerServices.Nullable(2)] T>(this Predicate<T> predicate) { return predicate.ChangeType<Converter<T, bool>>(); } private static bool Negate<T>(this Predicate<T> predicate, T obj) { return !predicate(obj); } public static Predicate<T> Negate<[System.Runtime.CompilerServices.Nullable(2)] T>(this Predicate<T> predicate) { return predicate.Negate<T>; } public static Predicate<T> Or<[System.Runtime.CompilerServices.Nullable(2)] T>(this Predicate<T> left, Predicate<T> right) { return delegate(T input) { if (!left(input)) return right(input); return true; }; } public static Predicate<T> And<[System.Runtime.CompilerServices.Nullable(2)] T>(this Predicate<T> left, Predicate<T> right) { return delegate(T input) { if (left(input)) return right(input); return false; }; } public static Predicate<T> Xor<[System.Runtime.CompilerServices.Nullable(2)] T>(this Predicate<T> left, Predicate<T> right) { return (T input) => left(input) ^ right(input); } [return: System.Runtime.CompilerServices.Nullable(0)] public static Result<bool> TryInvoke<[System.Runtime.CompilerServices.Nullable(2)] T>(this Predicate<T> predicate, T obj) { try { return predicate(obj); } catch (Exception error) { return new Result<bool>(error); } } } }