You should be able to convert your object to and from logical
Implicit conversion
Your object to boolean:
public static implicit operator bool(InterlockedBool obj) { return obj.Value; }
Then a boolean for your object:
public static implicit operator InterlockedBool(bool obj) { return new InterlockedBool(obj); }
Then you can check it out:
InterlockedBool test1 = true; if (test1) {
Explicit conversion
If you want users of this class to know that a conversion is occurring, you can force an explicit action:
public static explicit operator bool(InterlockedBool obj) { return obj.Value; } public static explicit operator InterlockedBool(bool obj) { return new InterlockedBool(obj); }
Then you must explicitly specify your objects:
InterlockedBool test1 = (InterlockedBool)true; if ((bool)test1) {
EDIT (due to OP comment)
In the conversion from boolean to your object, I call a constructor that you did not mention, here is how I built it:
public InterlockedBool(bool Value) { this.Value = Value; }
Therefore, setting the value is guaranteed by thread safety.
source share