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

后端 未结 8 2148
北恋
北恋 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:25

    Here's the closest that I can think of:

    using System.Linq;
    public static bool IsAllowed(int userID)
    {
      return new Personnel[]
          { Personnel.JohnDoe, Personnel.JaneDoe }.Contains((Personnel)userID);
    }
    

提交回复
热议问题