Related Tags:
C# C# (pronounced "C-sharp") is an object-oriented programming language from Microsoft that aims to combine the computing power of C++ with the programming ease of Visual Basic. C# is based on C++ and contains features similar to those of Java. Learn More, Enum Flags Attribute The idea of Enum Flags is to take an enumeration variable and allow it hold multiple values. It should be used whenever the enum represents a collection of flags, rather than representing a single value.You create a bit flags enum by applying the System. Learn More,

How to check if the flags enum value has/contains other flags enum values in c#?

Description:For example I have Following flags enum and I have stored a number 7 in the database now I have to check if 7 contains the requested number let say 3(which is for OptionOne and OptionTwo) it should return true, in this way I have to compare a single int value with a single int value for the following flag enum.


[Flags]
public enum MyFlagsEnum
{
None = 0,
OptionOne = 1,
OptionTwo = 2,
OptionThree = 4,
}

Posted by: | Posted on: Aug 21, 2020

1 answers

Replies

4

You can easily check that by using the following code

int valueFromDatabase = 7;
int requestedValue = 3;
if (((MyFlagsEnum)valueFromDatabase).HasFlag((MyFlagsEnum)requestedValue))
{
//perform your operations here
return true;
}
else
{
return false;
}

Replied by: | Replied on: Aug 21, 2020



Reply
×

Code block Hyperlink bold Quotes block Upload Images

Preview