Iterate over an Enum subgroup I'm looking for a way to get a subset of a given enum to avoid repetition and use same enum type while comparing.

I found this question Enum subset or subgroup in C# and like the idea of using static class. Whoever, It doesn't allow to iterate over all elements in the class. So I added GetValues method. 

Edit:

Here is my full code:

    public partial class Devices : Form
        {
            public Devices()
            {
                InitializeComponent();

                // Here, I need to iterate over StatusEnum items
                foreach (var item in Enum.GetValues(typeof(StatusEnum)))
                {
                    checkedListBoxParent.Items.Add(item);
                }

                // and iterate over ValidStatusEnum items
                foreach (var item in ValidStatusEnum.GetValues())
                {
                    checkedListBoxChild.Items.Add(item);
                }
            }

            private void On_MouseUp(object sender, MouseEventArgs e)
            {
                // Here, I need to compare StatusEnum and ValidStatusEnum
                if (Equals(checkedListBoxParent.CheckedItems.Cast&lt;StatusEnum&gt;().ToList(),
                    checkedListBoxChild.CheckedItems.Cast&lt;StatusEnum&gt;().ToList()))
                {
                    MessageBox.Show("Parent and child are similar");
                }
            }

            private bool Equals(List&lt;StatusEnum&gt; list1, List&lt;StatusEnum&gt; list2)
            {
                if (list1.Count != list2.Count)
                    return false;
                for (int i = 0; i &lt; list1.Count; i++)
                {
                    if (list1[i] != list2[i])
                        return false;
                }
                return true;
            }

            private bool Equals(StatusEnum status, StatusEnum validStatus)
            {
                return status == validStatus;
            }
        }

        public enum StatusEnum { Unknown, Known, NotAvailable, Available };

        internal static class ValidStatusEnum
        {
            public const StatusEnum Known = StatusEnum.Known;
            public const StatusEnum Available = StatusEnum.Available;

            public static StatusEnum[] GetValues()
            {
                return new StatusEnum[] { Known, Available };
            }
        }
}


So basically, in my winforms application, I have two CheckedListBoxes (Parent and Child) populated from StatusEnum Enum and ValidStatusEnum static class.
So, I need to iterate over enums to populate Checkboxes and I need to compare StatusEnum and ValidStatusEnum to display a message to user in case I have the same selection in both checkboxes.

Any thoughts about the design and the code?
This answer assumes there's no way the different "groups" would actually belong in different enum types. Otherwise the only sensible answer is "if you have different groups of related values, put them in different enum types". Status is a fairly broad name (enum types shouldn't have "Enum" in their names) that doesn't really says anything - if you have an Availability group with NotAvailable and Available possible values, then have an Availability enum with these values.



That's [very] little more than a hard-coded Enum.GetValues helper...

Define your enum vertically:

public enum Status
{
    Unknown,
    Known,
    NotAvailable,
    Available,
}


Then it's much easier to add attributes, e.g. a [Description]:

public enum Status
{
    [Description("Device is unknown.")]
    Unknown,
    [Description("Device is known.")]
    Known,
    [Description("Device is not available.")]
    NotAvailable,
    [Description("Device is available.")]
    Available,
}


Or whatever. If you have enum values that semantically related and need to regroup them, you can use flags for that. Use powers of 2 to specify explicit underlying values for each enum; you can use bit-shifting to simplify that:

[Flags]
public enum Status
{
    Unknown = 1 &lt;&lt; 0,
    Known = 1 &lt;&lt; 1,
    NotAvailable = 1 &lt;&lt; 2,
    Available = 1 &lt;&lt; 3,
}


And then you can define the "groupings" as you please:

[Flags]
public enum Status
{
    Unknown = 1 &lt;&lt; 0 | Knowledge | Negative,
    Known = 1 &lt;&lt; 1 | Knowledge | Positive,
    NotAvailable = 1 &lt;&lt; 2 | Availability | Negative,
    Available = 1 &lt;&lt; 3 | Availability | Positive,
    Knowledge = 1 &lt;&lt; 20,
    Availability = 1 &lt;&lt; 21,
    Positive = 1 &lt;&lt; 22,
    Negative = 1 &lt;&lt; 23,
}


And now when you're faced with a Status value, you can do this:

var isPositive = value.HasFlag(Status.Positive);


And you can get all Positive enum values rather easily:

var positiveStatusCodes = Enum.GetValues(typeof(Status))
                              .Cast&lt;Status&gt;()
                              .Where(status =&gt; status.HasFlag(Status.Positive))
                              .ToArray();


That way when you need to add enum values, just give them the proper flags and your "get all positive status codes" code doesn't need to change at all.