Shorthand conditional in C# similar to SQL 'in' keyword

后端 未结 8 2230
北恋
北恋 2021-01-04 12:44

In C# is there a shorthand way to write this:

public static bool IsAllowed(int userID)
{
    return (userID == Personnel.JohnDoe || userID == Personnel.JaneD         


        
8条回答
  •  情书的邮戳
    2021-01-04 13:29

    Are permissions user-id based? If so, you may end up with a better solution by going to role based permissions. Or you may end up having to edit that method quite frequently to add additional users to the "allowed users" list.

    For example, enum UserRole { User, Administrator, LordEmperor }

    class User {
        public UserRole Role{get; set;}
        public string Name {get; set;}
        public int UserId {get; set;}
    }
    
    public static bool IsAllowed(User user) {
        return user.Role == UserRole.LordEmperor;
    }
    

提交回复
热议问题