Why do many WPF-classes store boolean values as enum-flags?

218 Views Asked by At

If you reflect over the WPF System.Windows.Controls.Control class you can see:

public class Control : FrameworkElement
{ 
    internal ControlBoolFlags _controlBoolField;
    // [...]
    internal bool ReadControlFlag(ControlBoolFlags reqFlag);
    internal void WriteControlFlag(ControlBoolFlags reqFlag, bool set); 
    // I think it's clear what they do
    // [...]
    internal enum ControlBoolFlags : ushort
    {
        CommandDisabled = 8,
        ContainsSelection = 128,
        ContentIsItem = 16,
        ContentIsNotLogical = 1,
        HeaderIsItem = 32,
        HeaderIsNotLogical = 4,
        IsSpaceKeyDown = 2,
        ScrollHostValid = 64,
        VisualStateChangeSuspended = 256
    }
}

So I just want to know why MS chose this way to store the boolean values instead of using a bool field for every flag. Is it just their style of coding oder did they just want to save space for every field?

1

There are 1 best solutions below

0
On BEST ANSWER

There's several reason. A couple include:

  • It's a huge space-saver. 1 short representing many (9 in this example) possiblities. That's the size of single ushort rather than many bools
  • You can pass all those values to a method in a single parameter rather than having many parameters and a bunch of overloaded methods.