Skip to content

Commit d8d70e6

Browse files
committed
Add boolean methods
1 parent 679ce24 commit d8d70e6

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

SharpHelpers/SharpHelpers/BooleanHelper.cs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// (c) 2019 SharpCoding
22
// This code is licensed under MIT license (see LICENSE.txt for details)
3+
using System;
4+
35
namespace SharpCoding.SharpHelpers
46
{
57
public static class BooleanHelper
@@ -49,5 +51,51 @@ public static string ToStringValues(this bool? value, string trueValue, string f
4951
{
5052
return value.HasValue ? (value.Value ? trueValue : falseValue) : falseValue;
5153
}
54+
55+
/// <summary>
56+
/// Negates the instance boolean value.
57+
/// </summary>
58+
/// <param name="instance">The boolean value to negate.</param>
59+
/// <returns>Returns the negated boolean value.</returns>
60+
public static bool Not(this bool instance)
61+
{
62+
return !instance;
63+
}
64+
65+
/// <summary>
66+
/// Determines if the instance is true and executes the specified action if true.
67+
/// </summary>
68+
/// <param name="instance">The boolean value to evaluate.</param>
69+
/// <param name="action">The action to execute if the boolean value is true.</param>
70+
public static void IfTrue(this bool instance, Action action)
71+
{
72+
if (instance)
73+
{
74+
action?.Invoke();
75+
}
76+
}
77+
78+
/// <summary>
79+
/// Determines if the instance is false and executes the specified action if false.
80+
/// </summary>
81+
/// <param name="instance">The boolean value to evaluate.</param>
82+
/// <param name="action">The action to execute if the boolean value is false.</param>
83+
public static void IfFalse(this bool instance, Action action)
84+
{
85+
if (!instance)
86+
{
87+
action?.Invoke();
88+
}
89+
}
90+
91+
/// <summary>
92+
/// Returns the boolean value as an integer (1 for true, 0 for false).
93+
/// </summary>
94+
/// <param name="instance">The boolean value to convert.</param>
95+
/// <returns>1 if true, 0 if false.</returns>
96+
public static int ToInt(this bool instance)
97+
{
98+
return instance ? 1 : 0;
99+
}
52100
}
53101
}

0 commit comments

Comments
 (0)