Note that there are some explanatory texts on larger screens.

plurals
  1. POMy first extension method, could it be written better?
    text
    copied!<p>Because this is my first attempt at an extension method that seems quite useful to me, I just want to make sure I'm going down the right route </p> <pre><code> public static bool EqualsAny(this string s, string[] tokens, StringComparison comparisonType) { foreach (string token in tokens) { if (s.Equals(token, comparisonType)) { return true; } } return false; } </code></pre> <p>Called by</p> <pre><code>if (queryString["secure"].EqualsAny(new string[] {"true","1"}, StringComparison.InvariantCultureIgnoreCase)) { parameters.Protocol = Protocol.https; } </code></pre> <p><strong>EDIT:</strong> Some excellent suggestions coming through, exactly the sort of thing I was looking for. Thanks</p> <p><strong>EDIT:</strong></p> <p>I have decided on the following implementation</p> <pre><code>public static bool EqualsAny(this string s, StringComparison comparisonType, params string[] tokens) { // for the scenario it is more suitable for the code to continue if (s == null) return false; return tokens.Any(x =&gt; s.Equals(x, comparisonType)); } public static bool EqualsAny(this string s, params string[] tokens) { return EqualsAny(s, StringComparison.OrdinalIgnoreCase, tokens); } </code></pre> <p>I preferred using params over IEnumerable because it simplified the calling code</p> <pre><code>if (queryString["secure"].EqualsAny("true","1")) { parameters.Protocol = Protocol.https; } </code></pre> <p>A far cry on the previous</p> <pre><code>if (queryString["secure"] != null) { if (queryString["secure"] == "true" || queryString["secure"] == "1") { parameters.Protocal = Protocal.https; } } </code></pre> <p>Thank you again!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload