Should “or” work with .Net4 Hasflags: enum.HasFlag(AccessRights.Read | AccessRights.Write)

后端 未结 4 705
自闭症患者
自闭症患者 2020-12-31 06:58

I am trying out the new HasFlags features, and was wondering if the following should work:

enum.HasFlag(AccessRights.Read | AccessRights.Writ

4条回答
  •  既然无缘
    2020-12-31 07:29

    Alternatively, you could just reverse the order of the expression:

    //returns true - a bit easier on the eye
    (AccessRights.Read | AccessRights.Write).HasFlag(myAccessRights)
    

    This will return true if you have either Read | Write access. That would be functionally equivalent to:

    //also returns true - as per @Ani's answer
    myAccessRights & (AccessRights.Read | AccessRights.Write) != 0
    

    EDIT

    As pointed out in the comments, The first expression will return true if myAccessRights is empty, and false if myAccessRights has more than just Read and Write.

提交回复
热议问题