|
1 | 1 | // (c) 2019 SharpCoding |
2 | 2 | // This code is licensed under MIT license (see LICENSE.txt for details) |
| 3 | +using System; |
| 4 | + |
3 | 5 | namespace SharpCoding.SharpHelpers |
4 | 6 | { |
5 | 7 | public static class BooleanHelper |
@@ -49,5 +51,51 @@ public static string ToStringValues(this bool? value, string trueValue, string f |
49 | 51 | { |
50 | 52 | return value.HasValue ? (value.Value ? trueValue : falseValue) : falseValue; |
51 | 53 | } |
| 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 | + } |
52 | 100 | } |
53 | 101 | } |
0 commit comments