How can I compare enums?

Hi

It seams a bit stupid question but I have some problems comparing enums, imagine the following code

enum Actions {
    Read,
    Write,
    Delete,
    Query,
    Sync
}

Actions actions = Actions.Read | Actions.Write;


Now how can I use the IF statement to see if the (actions) contains the Actions.Read

This code for example is not working

If(actions == Actions.Read)

Is there is any method for contain something like

If(actions contains(Actions.Read)) 


Answer this question

How can I compare enums?

  • Sovman

    Hi

    try this:

    if((actions & Actions.Read)!= 0)
    {
    ....
    }

    Gogou

  • FugersonHall

    Thanks, It worked :)
  • Heathcliff

    What you need to do is use:

    if ((action & Actions.Write) == Actions.Write) ...

    -Karl Erickson

  • How can I compare enums?