Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 816
HerpDerpington Avatar asked Mar 23 '23 20:03

HerpDerpington


1 Answers

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.
like image 124
John Kraft Avatar answered Apr 03 '23 05:04

John Kraft