Condition Class
Definition
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: |
Methods
| Name | Description |
|---|---|
| AreEqual<T>(T, T) | Determines whether the two specified |
| AreEqual<T>(T, T, IEqualityComparer<T>) | Determines whether the two specified |
| AreNotEqual<T>(T, T) | Determines whether the two specified |
| AreNotEqual<T>(T, T, IEqualityComparer<T>) | Determines whether the two specified |
| AreNotSame<T>(T, T) | Determines whether the two specified |
| AreSame<T>(T, T) | Determines whether the two specified |
| FlipFlop(bool, Action, Action) | Invokes one of two expressions depending on the value of |
| FlipFlopAsync(bool, Func<Task>, Func<Task>) | Invokes one of two expressions depending on the value of |
| FlipFlop<T>(bool, Action<T>, Action<T>, T) | Invokes one of two expressions depending on the value of |
| FlipFlop<T1, T2>(bool, Action<T1, T2>, Action<T1, T2>, T1, T2) | Invokes one of two expressions depending on the value of |
| 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 |
| 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 |
| 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 |
| HasConsecutiveCharacters(string, char, int) | Determines whether the specified |
| HasConsecutiveCharacters(string, IEnumerable<char>, int) | Determines whether the specified |
| HasDifference(string, string, out string) | Determines whether there is a set difference between |
| IsBase64(string) | Determines whether the specified |
| IsBinaryDigits(string) | Determines whether the specified |
| IsCountableSequence(IEnumerable<int>) | Determines whether the specified |
| IsCountableSequence(IEnumerable<long>) | Determines whether the specified |
| IsCountableSequence(string) | Determines whether the specified |
| IsDefault<T>(T) | Determines whether the specified |
| IsEmailAddress(string) | Determines whether the specified |
| IsEmpty(string) | Determines whether the specified |
| IsEnum<T>(string, Action<EnumStringOptions>) | Determines whether the specified |
| IsEven(int) | Determines whether the specified |
| IsFalse(bool) | Determines whether the specified |
| IsFalse(bool, Action) | Invokes the delegate |
| IsGreaterThanOrEqual<T>(T, T) | Determines whether the specified |
| IsGreaterThan<T>(T, T) | Determines whether the specified |
| IsGuid(string) | Determines whether the specified |
| IsGuid(string, GuidFormats) | Determines whether the specified |
| IsHex(char) | Determines whether the specified |
| IsHex(string) | Determines whether the specified |
| IsLowerThanOrEqual<T>(T, T) | Determines whether the specified |
| IsLowerThan<T>(T, T) | Determines whether the specified |
| IsNotDefault<T>(T) | Determines whether the specified |
| IsNotNull<T>(T) | Determines whether the specified |
| IsNotWithinRange<T>(T, T, T) | Determines whether the specified |
| IsNull<T>(T) | Determines whether the specified |
| IsNumeric(string, NumberStyles, IFormatProvider) | Determines whether the specified value can be evaluated as a number. |
| IsOdd(int) | Determines whether the specified |
| IsPrime(int) | Determines whether the specified |
| IsProtocolRelativeUrl(string, Action<ProtocolRelativeUriStringOptions>) | Determines whether the specified |
| IsTrue(bool) | Determines whether the specified |
| IsTrue(bool, Action) | Invokes the delegate |
| IsUri(string, Action<UriStringOptions>) | Determines whether the specified |
| IsWhiteSpace(string) | Determines whether the specified |
| IsWithinRange<T>(T, T, T) | Determines whether the specified |
| TernaryIf<TResult>(bool, Func<TResult>, Func<TResult>) | Returns one of two values depending on the value of |
| TernaryIf<T, TResult>(bool, Func<T, TResult>, Func<T, TResult>, T) | Returns one of two values depending on the value of |
| 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 |
| 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 |
| 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 |
| 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 |