Skip to content

Commit

Permalink
Add boolean methods
Browse files Browse the repository at this point in the history
  • Loading branch information
engineering87 committed Oct 7, 2024
1 parent 679ce24 commit d8d70e6
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions SharpHelpers/SharpHelpers/BooleanHelper.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// (c) 2019 SharpCoding
// This code is licensed under MIT license (see LICENSE.txt for details)
using System;

namespace SharpCoding.SharpHelpers
{
public static class BooleanHelper
Expand Down Expand Up @@ -49,5 +51,51 @@ public static string ToStringValues(this bool? value, string trueValue, string f
{
return value.HasValue ? (value.Value ? trueValue : falseValue) : falseValue;
}

/// <summary>
/// Negates the instance boolean value.
/// </summary>
/// <param name="instance">The boolean value to negate.</param>
/// <returns>Returns the negated boolean value.</returns>
public static bool Not(this bool instance)
{
return !instance;
}

/// <summary>
/// Determines if the instance is true and executes the specified action if true.
/// </summary>
/// <param name="instance">The boolean value to evaluate.</param>
/// <param name="action">The action to execute if the boolean value is true.</param>
public static void IfTrue(this bool instance, Action action)
{
if (instance)
{
action?.Invoke();
}
}

/// <summary>
/// Determines if the instance is false and executes the specified action if false.
/// </summary>
/// <param name="instance">The boolean value to evaluate.</param>
/// <param name="action">The action to execute if the boolean value is false.</param>
public static void IfFalse(this bool instance, Action action)
{
if (!instance)
{
action?.Invoke();
}
}

/// <summary>
/// Returns the boolean value as an integer (1 for true, 0 for false).
/// </summary>
/// <param name="instance">The boolean value to convert.</param>
/// <returns>1 if true, 0 if false.</returns>
public static int ToInt(this bool instance)
{
return instance ? 1 : 0;
}
}
}

0 comments on commit d8d70e6

Please sign in to comment.