-1

Not exactly a big deal to do this one myself, but I am curious if C# gives this to me anywhere:

public static IEnumerable<bool> AllBools {
  get {
    yield return false;
    yield return true;
  }
}
William Jockusch
  • 26,513
  • 49
  • 182
  • 323

1 Answers1

0

Here's the code, a bit clunkier than you probably want, but it works:

    public static IEnumerable<bool> BoolValues {
        get {
            return new bool[]{true, false};
        }
    }

Edit: if you want code to enumerate over all values of an enum (which would be a lot more useful, imo), here is also the code:

    public enum TrueOrFalse
    {
        True,
        False
    }

    public static IEnumerable<TrueOrFalse> BoolValues {
        get {
            List<TrueOrFalse> allValues = new List<TrueOrFalse>();
            foreach (var value in Enum.GetValues(typeof(TrueOrFalse))){
                allValues.Add((TrueOrFalse)(value));
            }

            return allValues.AsEnumerable();
        }
    }

Even simpler, as found here (How to get an array of all enum values in C#?):

 List<TrueOrFalse> valuesAsList =  Enum.GetValues(typeof(TrueOrFalse)).Cast<TrueOrFalse>().ToList();
Community
  • 1
  • 1
Chris Smeal
  • 681
  • 1
  • 6
  • 13