locked
When flag is used with Enum RRS feed

  • Question

  • User264732274 posted

    i use Enum many time but i like to know when people use flag attribute with Enum. please help me to understand the usage of flag with a example. thanks

    Friday, October 28, 2016 1:55 PM

Answers

  • User281315223 posted

    The use of Flags isn't really required, but it can be useful if you are going to be dealing with a collection of values that could be used together instead of a single value like this :

    // This indicates that your widget can be painted red, green or blue
    YourWidget.AccessiblePaintColors = ColorEnum.Red | ColorEnum.Green | ColorEnum.Blue;

    The Flags attribute also affects how the ToString() method is used when referring to an enum :

    enum Suits 
    { 
       Spades = 1, 
       Clubs = 2, 
       Diamonds = 3, 
       Hearts = 4 
    }
    
    [Flags] 
    enum FlaggedSuits
    { 
       Spades = 1, 
       Clubs = 2, 
       Diamonds = 3, 
       Hearts = 4 
    }
    
    // Output non-flagged combination 
    (Suits.Spades | Suits.Diamonds).ToString(); // yields "4" 
    // Output flagged combination
    (FlaggedSuits.Spades | FlaggedSuits.Diamonds).ToString(); // yields "Spades, Diamonds" 

    This related Stack Overflow discussion provides a great and quite detailed answer to what is going on behind the scenes when using the [Flags] attribute.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, October 28, 2016 2:30 PM
  • User-707554951 posted

    Hi sudip_inn,

    Using the [Flags] attribute on an enum allows to to perform bitwise operations on it so you can use to have settings for class.

    This is especially usefull when a class can have multiple settings applied to it.

    The [Flags] attribute on an enum allows you to assign multiple values to your enum at once

    For detailed description and example, you could refer to the following links:

    https://www.dotnetperls.com/enum-flags

    http://net-informations.com/faq/netfaq/flags.htm

    Best regards

    Cathy

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 31, 2016 2:56 AM

All replies

  • User281315223 posted

    The use of Flags isn't really required, but it can be useful if you are going to be dealing with a collection of values that could be used together instead of a single value like this :

    // This indicates that your widget can be painted red, green or blue
    YourWidget.AccessiblePaintColors = ColorEnum.Red | ColorEnum.Green | ColorEnum.Blue;

    The Flags attribute also affects how the ToString() method is used when referring to an enum :

    enum Suits 
    { 
       Spades = 1, 
       Clubs = 2, 
       Diamonds = 3, 
       Hearts = 4 
    }
    
    [Flags] 
    enum FlaggedSuits
    { 
       Spades = 1, 
       Clubs = 2, 
       Diamonds = 3, 
       Hearts = 4 
    }
    
    // Output non-flagged combination 
    (Suits.Spades | Suits.Diamonds).ToString(); // yields "4" 
    // Output flagged combination
    (FlaggedSuits.Spades | FlaggedSuits.Diamonds).ToString(); // yields "Spades, Diamonds" 

    This related Stack Overflow discussion provides a great and quite detailed answer to what is going on behind the scenes when using the [Flags] attribute.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, October 28, 2016 2:30 PM
  • User-707554951 posted

    Hi sudip_inn,

    Using the [Flags] attribute on an enum allows to to perform bitwise operations on it so you can use to have settings for class.

    This is especially usefull when a class can have multiple settings applied to it.

    The [Flags] attribute on an enum allows you to assign multiple values to your enum at once

    For detailed description and example, you could refer to the following links:

    https://www.dotnetperls.com/enum-flags

    http://net-informations.com/faq/netfaq/flags.htm

    Best regards

    Cathy

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 31, 2016 2:56 AM