using System.Collections.Generic;
public static void Main()
Console.WriteLine("By Text:");
Console.WriteLine(BoolEnum.TrySelect("Is True", BoolEnum.False).ToString());
Console.WriteLine(BoolEnum.TrySelect("Is Yo", BoolEnum.False).ToString());
Console.WriteLine(BoolEnum.TrySelect("Is False", BoolEnum.True).ToString());
Console.WriteLine(String.Empty);
Console.WriteLine("By Integer:");
Console.WriteLine(BoolEnum.TrySelect(1, BoolEnum.False).ToString());
Console.WriteLine(BoolEnum.TrySelect(0, BoolEnum.True).ToString());
Console.WriteLine(BoolEnum.TrySelect(11, BoolEnum.False).ToString());
Console.WriteLine(String.Empty);
Console.WriteLine("By Boolean:");
Console.WriteLine(BoolEnum.TrySelect(true, BoolEnum.False).ToString());
Console.WriteLine(BoolEnum.TrySelect(false, BoolEnum.True).ToString());
Console.WriteLine(String.Empty);
private static Dictionary<bool, BoolEnum> allValuesByNaturalValue = new Dictionary<bool, BoolEnum>();
private static Dictionary<string, BoolEnum> allValuesByTextValue = new Dictionary<string, BoolEnum>();
private static Dictionary<int, BoolEnum> allValuesByInteger = new Dictionary<int, BoolEnum>();
private int integerValue;
private bool naturalValue;
public static readonly BoolEnum True = new BoolEnum(true, "Is True", 1);
public static readonly BoolEnum False = new BoolEnum(false, "Is False", 0);
private BoolEnum(bool naturalValue, string boolText, int integerValue)
this.naturalValue = naturalValue;
this.boolText = boolText;
this.integerValue = integerValue;
allValuesByNaturalValue.Add(naturalValue, this);
allValuesByTextValue.Add(boolText, this);
allValuesByInteger.Add(integerValue, this);
public static BoolEnum TrySelect(bool naturalValue, BoolEnum defaultValue)
if (allValuesByNaturalValue.TryGetValue(naturalValue, out returnValue)) return returnValue;
public static BoolEnum TrySelect(string boolText, BoolEnum defaultValue)
if (allValuesByTextValue.TryGetValue(boolText, out returnValue)) return returnValue;
public static BoolEnum TrySelect(int integerValue, BoolEnum defaultValue)
if (allValuesByInteger.TryGetValue(integerValue, out returnValue)) return returnValue;
public static implicit operator bool(BoolEnum boolEnum)
return boolEnum != null ? boolEnum.naturalValue : false;
public static implicit operator string(BoolEnum boolEnum)
return boolEnum != null ? boolEnum.boolText : "Is False";
public static implicit operator int(BoolEnum boolEnum)
return boolEnum != null ? boolEnum.integerValue : 0;
public bool NaturalValue { get { return this.naturalValue; } }
public string BoolText { get { return this.boolText; } }
public int IntegerValue { get { return this.integerValue; } }
public static IReadOnlyCollection<BoolEnum> AllValues { get { return allValuesByNaturalValue.Values.ToList().AsReadOnly(); } }
public static IReadOnlyCollection<bool> AllBooleanValues { get { return allValuesByNaturalValue.Keys.ToList().AsReadOnly(); } }
public static IReadOnlyCollection<string> AllTextValues { get { return allValuesByTextValue.Keys.ToList().AsReadOnly(); } }
public static IReadOnlyCollection<int> AllIntegerValues { get { return allValuesByInteger.Keys.ToList().AsReadOnly(); } }
public override string ToString()
return "[" + this.naturalValue.ToString() + ", \"" + this.boolText.ToString() + "\", " + this.integerValue.ToString() + "]";