Skip to content
This repository has been archived by the owner on May 25, 2019. It is now read-only.

Flags enum design

Thang Chung edited this page Dec 11, 2017 · 1 revision
[Flags]
public enum MyColors
{
    Yellow = 1,
    Green = 2,
    Red = 4,
    Blue = 8
}

To retrieve the distinct values in your property, one can do this:

if((myProperties.AllowedColors & MyColor.Yellow) == MyColor.Yellow)
{
    // Yellow has been set...
}

if((myProperties.AllowedColors & MyColor.Green) == MyColor.Green)
{
    // Green has been set...
}    
or, in .NET 4 and later:

if (myProperties.AllowedColors.HasFlag(MyColor.Yellow))
{
    // Yellow has been set...
}

Reference to https://stackoverflow.com/questions/8447/what-does-the-flags-enum-attribute-mean-in-c

Clone this wiki locally