Table of Contents

Condition Class

Definition

Namespace
Cuemon
Assemblies
Cuemon.Kernel.dll
Source
src/Cuemon.Kernel/Condition.cs

Provide ways to verify conditions a generic way for countless scenarios using true/false propositions.

public sealed class Condition
Inheritance
Condition

Examples

The following example demonstrates how to use the Condition class to perform common validation checks, equality comparisons, conditional branching, and range assertions. It exercises AreEqual, AreNotEqual, and AreSame for equality, FlipFlop and TernaryIf for conditional execution, and validation helpers such as IsEmail, IsGuid, IsNumeric, IsPrime, and IsWithinRange. An async flip-flop variant confirms the API works with task-based delegates. Each result is written to the console, illustrating the full validation utility surface.

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Threading.Tasks;
using Cuemon;

namespace MyApp.Validation
{
    public class ConditionExamples
    {
        public void DemonstrateConditions()
        {
            // AreEqual / AreNotEqual with default and custom comparers
            if (Condition.AreEqual("hello", "hello"))
            {
                Console.WriteLine("Strings are equal using default ordinal comparison.");

            if (Condition.AreEqual("hello", "HELLO", StringComparer.OrdinalIgnoreCase))
            {
                Console.WriteLine("Strings are equal ignoring case.");

            if (Condition.AreNotEqual("hello", "world"))
            {
                Console.WriteLine("Strings are different.");

            // Reference equality checks
            var same = new object();
            var different = new object();
            Console.WriteLine(Condition.AreSame(same, same));   // True
            Console.WriteLine(Condition.AreSame(same, different)); // False

            // FlipFlop - executes one of two actions based on condition
            var log = new List<string>();
            Condition.FlipFlop(true,
                () => log.Add("condition was true"),
                () => log.Add("condition was false"));

            Condition.FlipFlop(false,
                (int x) => log.Add($"got {x}"),
                (int x) => log.Add($"skipped {x}"),
                42);

            Console.WriteLine(string.Join(", ", log)); // "condition was true, skipped 42"

            // TernaryIf - functional ternary
            var result = Condition.TernaryIf(true,
                () => "first branch",
                () => "second branch");
            Console.WriteLine(result); // "first branch"

            var guided = Condition.TernaryIf(42 > 10,
                (int x) => x * 2,
                (int x) => x / 2,
                42);
            Console.WriteLine(guided); // 84

            // IsTrue / IsFalse as conditional invocations
            Condition.IsTrue(Condition.IsEmailAddress("user@example.com"), () =>
            {
                Console.WriteLine("Valid email address.");
            });

            Condition.IsFalse(string.IsNullOrEmpty("hello"), () =>
            {
                Console.WriteLine("String is not null or empty.");
            });

            // Validation checks
            Console.WriteLine(Condition.IsGuid("550e8400-e29b-41d4-a716-446655440000")); // True
            Console.WriteLine(Condition.IsGuid("not-a-guid")); // False
            Console.WriteLine(Condition.IsUri("https://example.com")); // True
            Console.WriteLine(Condition.IsNumeric("3.14", NumberStyles.Float, CultureInfo.InvariantCulture)); // True
            Console.WriteLine(Condition.IsEmailAddress("test@test.com")); // True
            Console.WriteLine(Condition.IsEven(42)); // True
            Console.WriteLine(Condition.IsOdd(41)); // True
            Console.WriteLine(Condition.IsPrime(17)); // True
            Console.WriteLine(Condition.IsDefault(0)); // True
            Console.WriteLine(Condition.IsNotDefault(42)); // True
            Console.WriteLine(Condition.IsHex("FF00A1")); // True
            Console.WriteLine(Condition.IsBase64("SGVsbG8=")); // True

            // Range checks
            Console.WriteLine(Condition.IsWithinRange(5, 1, 10)); // True
            Console.WriteLine(Condition.IsNotWithinRange(15, 1, 10)); // True
            Console.WriteLine(Condition.IsGreaterThan(100, 50)); // True
            Console.WriteLine(Condition.IsLowerThan(3, 10)); // True

            // Consecutive characters
            Console.WriteLine(Condition.HasConsecutiveCharacters("bookkeeper", 'o')); // True
            Console.WriteLine(Condition.HasConsecutiveCharacters("abc", new[] { 'x', 'y' })); // False

            // Countable sequences
            Console.WriteLine(Condition.IsCountableSequence(new[] { 1, 3, 5, 7 })); // True
            Console.WriteLine(Condition.IsCountableSequence("abc")); // True

            // Async flip-flop
            var asyncLog = new List<string>();
            var task = Condition.FlipFlopAsync(true,
                async () => { asyncLog.Add("async:true"); await Task.CompletedTask; },
                async () => { asyncLog.Add("async:false"); await Task.CompletedTask; });
            task.Wait();
            Console.WriteLine(string.Join(", ", asyncLog)); // "async:true"

}}}}}
}

Properties

Name Description
Query

Gets the singleton instance of the Condition functionality allowing for extensions methods like: Condition.Query.IsTrue().

Methods

Name Description
AreEqual<T>(T, T)

Determines whether the two specified x and y are equal by using the default equality operator from T.

AreEqual<T>(T, T, IEqualityComparer<T>)

Determines whether the two specified x and y are equal by using the equality operator.

AreNotEqual<T>(T, T)

Determines whether the two specified x and y are different by using the default equality operator from T.

AreNotEqual<T>(T, T, IEqualityComparer<T>)

Determines whether the two specified x and y are different by using the equality operator.

AreNotSame<T>(T, T)

Determines whether the two specified x object are not of the same instance as the y object.

AreSame<T>(T, T)

Determines whether the two specified x object are of the same instance as the y object.

FlipFlop(bool, Action, Action)

Invokes one of two expressions depending on the value of condition.

FlipFlopAsync(bool, Func<Task>, Func<Task>)

Invokes one of two expressions depending on the value of condition.

FlipFlop<T>(bool, Action<T>, Action<T>, T)

Invokes one of two expressions depending on the value of condition.

FlipFlop<T1, T2>(bool, Action<T1, T2>, Action<T1, T2>, T1, T2)

Invokes one of two expressions depending on the value of condition.

FlipFlop<T1, T2, T3>(bool, Action<T1, T2, T3>, Action<T1, T2, T3>, T1, T2, T3)

Invokes one of two expressions depending on the value of condition.

FlipFlop<T1, T2, T3, T4>(bool, Action<T1, T2, T3, T4>, Action<T1, T2, T3, T4>, T1, T2, T3, T4)

Invokes one of two expressions depending on the value of condition.

FlipFlop<T1, T2, T3, T4, T5>(bool, Action<T1, T2, T3, T4, T5>, Action<T1, T2, T3, T4, T5>, T1, T2, T3, T4, T5)

Invokes one of two expressions depending on the value of condition.

HasConsecutiveCharacters(string, char, int)

Determines whether the specified value contains a succession character of length.

HasConsecutiveCharacters(string, IEnumerable<char>, int)

Determines whether the specified value contains at least one of the succession characters of length.

HasDifference(string, string, out string)

Determines whether there is a set difference between second and first.

IsBase64(string)

Determines whether the specified value matches a base-64 structure.

IsBinaryDigits(string)

Determines whether the specified value consists only of binary digits.

IsCountableSequence(IEnumerable<int>)

Determines whether the specified source is a sequence of countable integrals (hence, integrals being either incremented or decremented with the same cardinality through out the sequence).

IsCountableSequence(IEnumerable<long>)

Determines whether the specified source is a sequence of countable integrals (hence, integrals being either incremented or decremented with the same cardinality through out the sequence).

IsCountableSequence(string)

Determines whether the specified value is a sequence of countable characters (hence, characters being either incremented or decremented with the same cardinality through out the sequence).

IsDefault<T>(T)

Determines whether the specified value has its initial default value.

IsEmailAddress(string)

Determines whether the specified value has a valid format of an email address.

IsEmpty(string)

Determines whether the specified value is empty ("").

IsEnum<T>(string, Action<EnumStringOptions>)

Determines whether the specified value is valid by attempting to construct an enumeration of T.

IsEven(int)

Determines whether the specified value is an even number.

IsFalse(bool)

Determines whether the specified value is false.

IsFalse(bool, Action)

Invokes the delegate expression when value of condition is false.

IsGreaterThanOrEqual<T>(T, T)

Determines whether the specified x is greater than or equal to y.

IsGreaterThan<T>(T, T)

Determines whether the specified x is greater than y.

IsGuid(string)

Determines whether the specified value has a valid format of a Guid.

IsGuid(string, GuidFormats)

Determines whether the specified value has a valid format of a Guid.

IsHex(char)

Determines whether the specified value is hexadecimal.

IsHex(string)

Determines whether the specified value is hexadecimal.

IsLowerThanOrEqual<T>(T, T)

Determines whether the specified x is lower than or equal to y.

IsLowerThan<T>(T, T)

Determines whether the specified x is lower than y.

IsNotDefault<T>(T)

Determines whether the specified value does not have its initial default value.

IsNotNull<T>(T)

Determines whether the specified value is not null.

IsNotWithinRange<T>(T, T, T)

Determines whether the specified x is outside the range of min and max.

IsNull<T>(T)

Determines whether the specified value is null.

IsNumeric(string, NumberStyles, IFormatProvider)

Determines whether the specified value can be evaluated as a number.

IsOdd(int)

Determines whether the specified value is an odd number.

IsPrime(int)

Determines whether the specified value is a prime number.

IsProtocolRelativeUrl(string, Action<ProtocolRelativeUriStringOptions>)

Determines whether the specified value is valid by attempting to construct a URI.

IsTrue(bool)

Determines whether the specified value is true.

IsTrue(bool, Action)

Invokes the delegate expression when value of condition is true.

IsUri(string, Action<UriStringOptions>)

Determines whether the specified value is valid by attempting to construct a URI.

IsWhiteSpace(string)

Determines whether the specified value consist only of white-space characters.

IsWithinRange<T>(T, T, T)

Determines whether the specified x is within range of min and max.

TernaryIf<TResult>(bool, Func<TResult>, Func<TResult>)

Returns one of two values depending on the value of condition.

TernaryIf<T, TResult>(bool, Func<T, TResult>, Func<T, TResult>, T)

Returns one of two values depending on the value of condition.

TernaryIf<T1, T2, TResult>(bool, Func<T1, T2, TResult>, Func<T1, T2, TResult>, T1, T2)

Returns one of two values depending on the value of condition.

TernaryIf<T1, T2, T3, TResult>(bool, Func<T1, T2, T3, TResult>, Func<T1, T2, T3, TResult>, T1, T2, T3)

Returns one of two values depending on the value of condition.

TernaryIf<T1, T2, T3, T4, TResult>(bool, Func<T1, T2, T3, T4, TResult>, Func<T1, T2, T3, T4, TResult>, T1, T2, T3, T4)

Returns one of two values depending on the value of condition.

TernaryIf<T1, T2, T3, T4, T5, TResult>(bool, Func<T1, T2, T3, T4, T5, TResult>, Func<T1, T2, T3, T4, T5, TResult>, T1, T2, T3, T4, T5)

Returns one of two values depending on the value of condition.