<p>In C# is there a shorthand way to write this:</p>

<pre><code>public static bool IsAllowed(int userID)
{
    return (userID == Personnel.JohnDoe || userID == Personnel.JaneDoe ...);
}
</code></pre>

<p>Like:</p>

<pre><code>public static bool IsAllowed(int userID)
{
    return (userID in Personnel.JohnDoe, Personnel.JaneDoe ...);
}
</code></pre>

<p>I know I could also use switch, but there are probably 50 or so functions like this I have to write (porting a classic ASP site over to ASP.NET) so I'd like to keep them as short as possible.</p>

[{'answer_id': 32942, 'author': 'Frank Krueger', 'author_id': 338, 'author_profile': 'https://Stackoverflow.com/users/338', 'pm_score': 2, 'selected': False, 'text': '<p>I would encapsulate the list of allowed IDs as <em>data</em> not <em>code</em>. Then it\'s source can be changed easily later on.</p>\n\n<pre><code>List&lt;int&gt; allowedIDs = ...;\n\npublic bool IsAllowed(int userID)\n{\n    return allowedIDs.Contains(userID);\n}\n</code></pre>\n\n<p>If using .NET 3.5, you can use <code>IEnumerable</code> instead of <code>List</code> thanks to extension methods.</p>\n\n<p>(This function shouldn\'t be static. See this posting: <a href="https://stackoverflow.com/questions/30998/using-too-much-static-bad-or-good#31004">using too much static bad or good ?</a>.)</p>\n'}, {'answer_id': 32944, 'author': 'Yaakov Ellis', 'author_id': 51, 'author_profile': 'https://Stackoverflow.com/users/51', 'pm_score': 2, 'selected': False, 'text': '<p>How about something like this:</p>\n\n<pre><code>public static bool IsAllowed(int userID) {\n  List&lt;int&gt; IDs = new List&lt;string&gt; { 1,2,3,4,5 };\n  return IDs.Contains(userID);\n}\n</code></pre>\n\n<p>(You could of course change the static status, initialize the IDs class in some other place, use an IEnumerable&lt;>, etc, based on your needs. The main point is that the closest equivalent to the <em>in</em> operator in SQL is the Collection.Contains() function.)</p>\n'}, {'answer_id': 32950, 'author': 'Pauly', 'author_id': 3460, 'author_profile': 'https://Stackoverflow.com/users/3460', 'pm_score': 1, 'selected': False, 'text': '<p>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.</p>\n\n<p>For example, \n    enum UserRole {\n        User, Administrator, LordEmperor\n    }</p>\n\n<pre><code>class User {\n    public UserRole Role{get; set;}\n    public string Name {get; set;}\n    public int UserId {get; set;}\n}\n\npublic static bool IsAllowed(User user) {\n    return user.Role == UserRole.LordEmperor;\n}\n</code></pre>\n'}, {'answer_id': 33014, 'author': 'ljs', 'author_id': 3394, 'author_profile': 'https://Stackoverflow.com/users/3394', 'pm_score': 0, 'selected': False, 'text': "<p>A nice little trick is to sort of reverse the way you usually use .Contains(), like:-</p>\n\n<pre><code>public static bool IsAllowed(int userID) {\n  return new int[] { Personnel.JaneDoe, Personnel.JohnDoe }.Contains(userID);\n}\n</code></pre>\n\n<p>Where you can put as many entries in the array as you like.</p>\n\n<p>If the Personnel.x is an enum you'd have some casting issues with this (and with the original code you posted), and in that case it'd be easier to use:-</p>\n\n<pre><code>public static bool IsAllowed(int userID) {\n  return Enum.IsDefined(typeof(Personnel), userID);\n}\n</code></pre>\n"}, {'answer_id': 33015, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': "<p>Here's the closest that I can think of:</p>\n\n<pre><code>using System.Linq;\npublic static bool IsAllowed(int userID)\n{\n  return new Personnel[]\n      { Personnel.JohnDoe, Personnel.JaneDoe }.Contains((Personnel)userID);\n}\n</code></pre>\n"}, {'answer_id': 33018, 'author': 'Adam', 'author_id': 3142, 'author_profile': 'https://Stackoverflow.com/users/3142', 'pm_score': 0, 'selected': False, 'text': '<p>Just another syntax idea:</p>\n\n<pre><code>return new [] { Personnel.JohnDoe, Personnel.JaneDoe }.Contains(userID);\n</code></pre>\n'}, {'answer_id': 33024, 'author': 'Chad Boyer', 'author_id': 3135, 'author_profile': 'https://Stackoverflow.com/users/3135', 'pm_score': 0, 'selected': False, 'text': '<p>Can you write an iterator for Personnel.</p>\n\n<pre><code>public static bool IsAllowed(int userID)\n{\n    return (Personnel.Contains(userID))\n}\n\npublic bool Contains(int userID) : extends Personnel (i think that is how it is written)\n{\n    foreach (int id in Personnel)\n        if (id == userid)\n            return true;\n    return false;\n}\n</code></pre>\n'}, {'answer_id': 33234, 'author': 'Jon Sagara', 'author_id': 731, 'author_profile': 'https://Stackoverflow.com/users/731', 'pm_score': 5, 'selected': True, 'text': "<p>How about this?</p>\n\n<pre><code>public static class Extensions\n{\n    public static bool In&lt;T&gt;(this T testValue, params T[] values)\n    {\n        return values.Contains(testValue);\n    }\n}\n</code></pre>\n\n<p>Usage:</p>\n\n<pre><code>Personnel userId = Personnel.JohnDoe;\n\nif (userId.In(Personnel.JohnDoe, Personnel.JaneDoe))\n{\n    // Do something\n}\n</code></pre>\n\n<p>I can't claim credit for this, but I also can't remember where I saw it.  So, credit to you, anonymous Internet stranger.</p>\n"}]
['https://Stackoverflow.com/questions/32937', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1302/']